summaryrefslogtreecommitdiff
path: root/git-fork
blob: 25d074fc32ad609ce886d0905abd43a71d1e1103 (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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import subprocess
import sys
import os


try:
    output = subprocess.check_output('git config fork.username', shell=True)
    username = output.decode('utf-8').strip()
except:
    username = os.environ['USER']

if len(sys.argv) == 2 and sys.argv[1] == '--public':
    remote_host = '%s@git.collabora.co.uk' % username
    git_remote_name = 'collabora'
    url = 'git://git.collabora.co.uk/git/user/%s/' % username
    push_url = 'git+ssh://%s/git/user/%s/' % (remote_host, username)
    update_cmd = ('sudo -u gitweb /root/dafR/update-user-git-links '
                  '/srv/git.collabora.co.uk/git/user')
elif len(sys.argv) == 1 or sys.argv[1] == '--private':
    remote_host = '%s@git.internal.collabora.co.uk' % username
    git_remote_name = 'collabora-internal'
    push_url = url = 'git+ssh://%s/git/users/%s/' % (remote_host, username)
    update_cmd = 'sudo -u cgit /srv/git.internal.collabora.co.uk/gen-repos.sh'
else:
    print("Error: Only --public or --private options are recognised.")
    sys.exit(1)

# Get current git repository's name
output = subprocess.check_output(['git', 'rev-parse', '--show-toplevel'])
path = output.decode('utf-8').strip()
name = path[path.rfind('/') + 1:]
if not name.endswith('.git'):
    name = name + '.git'

# Create a remote repository
remote_cmd = 'cd ~/public_html/git'
remote_cmd += ' && git init --bare ' + name
remote_cmd += ' && ' + update_cmd
subprocess.check_call(['ssh', remote_host, remote_cmd])

# Add the remote repository to git remotes
subprocess.check_call(['git', 'remote', 'add', git_remote_name, url + name])
if push_url != url:
    subprocess.check_call(['git', 'remote', 'set-url', '--push',
                           git_remote_name, push_url + name])
subprocess.check_call(['git', 'push', git_remote_name, 'master'])