Instantly share code, notes, and snippets.

Embed
What would you like to do?
python script for setting up git directories on remote servers
#
# This script creates a repository and sets it up with a post receive
# hook that checks out the code to the desired directory.
#
# Really nice for setting up an easy way to push code to a remote
# server without lots of overhead.
#
# After running this script simply add a remote locally like
#
# git remote add web ssh://you@server/path/to/repo.git
#
# Then pushing to the remote is as easy as
#
# git push web
#
# Jeremy Keeshin
# September 23, 2012
# updated March 31, 2013
# updated December 1, 2013
# - print out resulting command
#
import os
import sys
def main():
if len(sys.argv) != 3:
print """
Usage: python create_repo.py repo.git /your/checkout/dirctory
Locally, run:
git remote add web ssh://jkeesh@thekeesh.com/home/jkeesh/repos/<repo.git>
To deploy, use:
git push web master
"""
sys.exit(1)
else:
repo = sys.argv[1]
checkout_dir = sys.argv[2]
# Make the git directory
os.system('mkdir %s' % repo)
os.chdir('%s' % repo)
# Initialize a new bare repo
os.system('git init --bare')
txt = """#!/bin/sh
GIT_WORK_TREE=%s git checkout -f""" % checkout_dir
# Write the post-receive hook
hook = open('./hooks/post-receive', 'w')
hook.write(txt)
hook.close()
# Make it executable
os.system('chmod +x ./hooks/post-receive')
# Print a command to run next.
print "\n\nNow run this command on your local repository\n\n"
cmd = 'git remote add web ssh://jkeesh@thekeesh.com/home/jkeesh/repos/' + repo
print cmd
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment