forked from gitlabhq/gitlabhq
/
remotes
executable file
·174 lines (149 loc) · 4.63 KB
/
remotes
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
#!/usr/bin/env python
import os
import sys
import json
import errno
import atexit
import shutil
import tempfile
import StringIO
import subprocess
try:
command = sys.argv[1]
if command == 'list':
project = sys.argv[2]
elif command == 'add':
project, remote, giturl = sys.argv[2:]
elif command == 'del':
project, remote = sys.argv[2:]
elif command == 'clone':
project, remote, giturl = sys.argv[2:]
else:
raise ValueError('Invalid command: %s' % command)
except (ValueError, TypeError):
print >> sys.stderr, 'Usage: %s COMMAND [ARGUMENTS...]' % sys.argv[0]
print >> sys.stderr, 'COMMANDs:'
print >> sys.stderr, ' list PROJECT REMOTE GIT-URL' % sys.argv[0]
print >> sys.stderr, ' add PROJECT REMOTE GIT-URL' % sys.argv[0]
print >> sys.stderr, ' del PROJECT REMOTE' % sys.argv[0]
print >> sys.stderr, ' clone PROJECT REMOTE GIT-URL' % sys.argv[0]
sys.exit(1)
getup_config_flename = 'getup-conf'
git = os.environ['GIT_PATH'] if 'GIT_PATH' in os.environ else '/usr/bin/git'
_empty_project = {
'project': '',
'remotes': [],
}
def json_dump(obj):
data = StringIO.StringIO()
json.dump(obj, data, indent=3)
data.seek(0)
return data.read()
def print_status(status=500, **kva):
if kva:
print json_dump({ 'status': status, 'data': kva })
else:
print json_dump({ 'status': status })
def project_dir(project):
return os.path.expanduser('~git/repositories/%s.git' % project)
def config_path(project):
return os.path.join(project_dir(project), getup_config_flename)
def chdir_project(project):
gitdir = project_dir(project)
try:
os.chdir(gitdir)
except OSError, ex:
print_status(status=404 if ex.errno == errno.ENOENT else 500, directory=gitdir)
sys.exit(2)
def lock_config(project):
pass
def release_config(project):
pass
def load_config(project):
with open(config_path(project), 'a+') as fp:
content = None
try:
content = fp.read()
config = json.loads(content)
#print 'loaded config: %s' % fp.name
except ValueError, ex:
if content:
raise ValueError("Invalid config file format: %s", ex)
config = dict(_empty_project)
config['project'] = project
assert 'project' in config, 'Missing entry: project'
assert 'remotes' in config, 'Missing entry: remotes'
assert config['project'] == project, 'Mismatch project name'
return config
def save_config(config):
assert isinstance(config, dict)
with open(config_path(project), 'w+') as fp:
#print 'saving config: %s' % fp.name
data = json_dump(config)
fp.write(data)
#print 'config saved: %s' % data
#
# Commands
#
def add_remote(config, remote, giturl):
assert isinstance(config, dict)
remotes = config['remotes']
for r in remotes:
if remote == r['name']:
r['url'] = giturl
return
remotes.append({'name': remote, 'url': giturl})
def del_remote(config, remote):
assert isinstance(config, dict)
remotes = config['remotes']
before = len(remotes)
for i, r in enumerate(remotes):
if remote == r['name']:
remotes.pop(i)
if len(remotes) == before:
print_status(status=404, name=remote)
else:
print_status(status=204)
def list_remotes(config, status=200):
print_status(status=status, config=config)
def run_command(*arguments, **kvargs):
proc = subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kvargs)
stdout, stderr = proc.communicate()
if proc.returncode != 0:
print_status(status=500, command=arguments, retcode=proc.returncode, stdout=stdout, stderr=stderr)
raise Exception
return proc
def repo_is_empty():
arguments = [ git, 'log', '-1', '--pretty=format:%h' ]
proc = subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = proc.communicate()
return proc.returncode != 0 or not stdout
def clone_remote(config, remote, giturl):
assert config and giturl and project, 'Invalid parameters (%s, %s, %s)' % (config, project, giturl)
if not repo_is_empty():
print_status(status=409, message='project repository not empty')
sys.exit(2)
tempdir = tempfile.mkdtemp()
try:
run_command(git, 'clone', giturl, project, cwd=tempdir)
run_command(git, 'push', 'git@git.ops.getupcloud.com:%s.git' % project, 'master', cwd=os.path.join(tempdir, project))
except Exception:
sys.exit(2)
finally:
shutil.rmtree(tempdir, ignore_errors=False)
add_remote(config, remote, giturl)
list_remotes(config, status=201)
if command == 'list':
list_remotes(load_config(project))
sys.exit(0)
lock_config(project)
atexit.register(release_config, project)
config = load_config(project)
if command == 'add':
add_remote(config, remote, giturl)
list_remotes(config, status=201)
elif command == 'del':
del_remote(config, remote)
elif command == 'clone':
clone_remote(config, remote, giturl)
save_config(config)