Skip to content

Instantly share code, notes, and snippets.

@alexandr-san4ez
Last active September 18, 2020 06:56
Show Gist options
  • Save alexandr-san4ez/d740600560fd51e84be5e75d287184e2 to your computer and use it in GitHub Desktop.
Save alexandr-san4ez/d740600560fd51e84be5e75d287184e2 to your computer and use it in GitHub Desktop.
Creates a remote git repository
# -*- coding: UTF-8 -*-
import os
import stat
import argparse
from urllib.parse import urlunparse
from urllib.request import pathname2url
# wget https://gist.github.com/alexandr-san4ez/d740600560fd51e84be5e75d287184e2/raw/1dab00b6e4a9b4ff4a8b6e0771ef4b4f7a2c9d02/git-remote-deploy.py -q -O- | python3 - -h
def parse_args():
description = (
'Creates a remote git repository for this tutorial: '
'http://toroid.org/git-website-howto'
)
parser = argparse.ArgumentParser(description=description)
parser.add_argument('repos', help='path to the repository')
parser.add_argument('workdir', help='path to the working directory')
parser.add_argument('--post-receive', default=tuple(), metavar='comand', nargs='*',
help='the commands you need to run after getting the changes')
args = parser.parse_args()
if not args.repos.endswith('.git'):
args.repos = args.repos.rstrip(' .') + '.git'
return args
def init_git(repos):
os.mkdir(repos)
if os.system('git init --bare --quiet ' + repos) != 0:
raise Exception('git init finished with error')
def create_work_dir(workdir):
try:
os.mkdir(workdir)
except FileExistsError:
pass
def init_hook(repos, workdir, post_receive):
lines = [
'#!/bin/sh',
'GIT_WORK_TREE={} git checkout -f'.format(os.path.abspath(workdir)),
]
lines.extend(post_receive)
hooks = os.path.join(repos, 'hooks/post-receive')
with open(hooks, 'w') as file:
file.write('\n'.join(lines))
st = os.stat(hooks)
os.chmod(hooks, st.st_mode | stat.S_IEXEC) # add executable permission
def print_result(repos):
path = pathname2url(os.path.abspath(repos))
url = urlunparse(('ssh', 'username@server.com', path, None, None, None))
print('git remote add upstream ' + url)
print('git push upstream +master:refs/heads/master')
def main():
args = parse_args()
init_git(args.repos)
create_work_dir(args.workdir)
init_hook(args.repos, args.workdir, args.post_receive)
print_result(args.repos)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment