Created
March 2, 2015 09:45
-
-
Save sp3c73r2038/cc16cf57d12b436caf05 to your computer and use it in GitHub Desktop.
a simple script to simplify git hosting for personal usage.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
add ssh public key to
authorized_keys
file, and make sure/path/to/git-shell.py
is accessible to ssh user.