Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
a simple script to simplify git hosting for personal usage.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import re
import shlex
import sys
import subprocess
def main():
command_line = os.environ.get('SSH_ORIGINAL_COMMAND')
try:
shell(command_line)
except Exception as e:
print('', file=sys.stderr)
print(e, file=sys.stderr)
print('', file=sys.stderr)
def shell(command_line):
if not command_line:
raise ValueError('No command line provided')
args = shlex.split(command_line)
command = args[0]
repos_root = '/opt/git/repos'
print(args, file=sys.stderr)
if command in {'git-receive-pack', 'git-upload-pack', 'git-upload-archive'}:
args[1] = re.sub(r'\/+', '/', '{}/{}.git'.format(repos_root, args[1].replace('.git', '')))
print(args, file=sys.stderr)
if not os.path.isdir(args[1]):
init_git_repo(args[1])
print(args, file=sys.stderr)
os.execvp(command, args)
def init_git_repo(repo_dir):
os.makedirs(repo_dir)
p = subprocess.Popen('git init --bare', stdout=subprocess.PIPE,
stderr=subprocess.PIPE, cwd=repo_dir, shell=True)
stdout, stderr = p.communicate()
if __name__ == '__main__':
sys.exit(main())
@aleiphoenix

This comment has been minimized.

Copy link
Owner Author

aleiphoenix commented Mar 2, 2015

add ssh public key to authorized_keys file, and make sure /path/to/git-shell.py is accessible to ssh user.

command="/usr/bin/python3 /path/to/git-shell.py",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty KEY_TYPE PUB_KEY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.