Created
April 24, 2014 08:29
-
-
Save katyukha/11246501 to your computer and use it in GitHub Desktop.
# A simple bash script to share git repositories via standard git daemon application
This file contains 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/env bash | |
# | |
# A simple bash script to share git repositories via standard git daemon application | |
# run: "git_serv start [--listen <address>]" to start git daemon listening on address | |
# run: "git_serv share [repository_name]" inside a repository to share this repository. | |
# run: "git_serv del_share" inside a repository to stop sharing this repository | |
# run: "git_serv stop" to stop serving | |
# | |
# After first to steps your repository will be available to be cloned via command: | |
# "git clone git://<address>/repository_name | |
# | |
# Notes: | |
# - repository_name default is set to name of toplevel repository folder. | |
# - <address> default is set to listen on address(domain) returned by command linux shell command `hostname` | |
# | |
BASEDIR=~/.git_server | |
PIDFILE=$BASEDIR/run.pid | |
REPODIR=$BASEDIR/repositories/ | |
if [ ! -d "$BASEDIR" ]; then | |
mkdir $BASEDIR; | |
fi | |
if [ ! -d "$REPODIR" ]; then | |
mkdir $REPODIR; | |
fi | |
case "$1" in | |
"stop" ) | |
if [ -f $PIDFILE ]; then | |
kill `cat $PIDFILE` | |
rm -f $PIDFILE | |
fi | |
;; | |
"start" ) | |
if [ -f $PIDFILE ]; then | |
echo "Already running" | |
else | |
HOST=$(hostname) | |
if [ $2 = "--listen" ]; then | |
HOST=$3 | |
fi | |
echo "Listening to: $HOST" | |
git daemon --base-path=$REPODIR --export-all --detach --reuseaddr --listen=$HOST --pid-file=$PIDFILE | |
fi | |
;; | |
"share" ) | |
if git rev-parse --is-inside-work-tree; then | |
GITDIR=`git rev-parse --show-toplevel` | |
REPONAME=`basename $GITDIR` | |
if [ $2 != "" ]; then | |
REPONAME=$2 | |
fi | |
(cd $REPODIR && ln -s $GITDIR $REPONAME) | |
echo "repository $REPONAME: shared" | |
fi | |
;; | |
"del-share" ) | |
if git rev-parse --is-inside-work-tree; then | |
GITDIR=`git rev-parse --show-toplevel` | |
REPONAME=`basename $GITDIR` | |
(cd $REPODIR && rm -f $REPONAME) | |
echo "repository $REPONAME: stop sharing" | |
fi | |
;; | |
"" ) | |
echo "No command supplied"; | |
echo "available commands: start, stop, share, del-share"; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment