Skip to content

Instantly share code, notes, and snippets.

@sp3c73r2038
Created March 2, 2015 09:45
Show Gist options
  • Save sp3c73r2038/cc16cf57d12b436caf05 to your computer and use it in GitHub Desktop.
Save sp3c73r2038/cc16cf57d12b436caf05 to your computer and use it in GitHub Desktop.
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())
@sp3c73r2038
Copy link
Author

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