Skip to content

Instantly share code, notes, and snippets.

@crmne
Created May 28, 2010 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save crmne/417106 to your computer and use it in GitHub Desktop.
Save crmne/417106 to your computer and use it in GitHub Desktop.
Sharing Git repositories with one command http://iflipbits.com/645186828
#!/bin/bash -e
# Your user web directory, typically "${HOME}/public_html" or "${HOME}/Sites"
PUBLIC_HTML="${HOME}/Sites"
# Subfolder of $PUBLIC_HTML where to put your shared repositories
PUBLIC_GIT="git"
# Save the shared repository with this remote name
REMOTE="public"
# The branch that will be pushed to your shared repository
BRANCH="master"
function get_public_paths () {
repo_name="$(basename "$top_level_dir").git"
public_dir="${PUBLIC_HTML}/${PUBLIC_GIT}/${repo_name}"
public_url="http://$(hostname)/~${USER}/${PUBLIC_GIT}/${repo_name}"
}
function get_top_level_dir () {
if [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" == "true" ]; then
top_level_dir="$(git rev-parse --show-toplevel)"
else
echo "This is not a directory managed by git"
exit 1
fi
}
# Takes the complete path of the public repo, including .git
function is_bare_repo () {
pushd "$1" > /dev/null
if [ "$(git rev-parse --is-inside-git-dir 2>/dev/null)" == "true" ]; then
if [ "$(git rev-parse --is-bare-repository 2>/dev/null)" == "true" ]; then
popd > /dev/null
return 0
fi
fi
popd > /dev/null
return 1
}
# Takes the complete path of the public repo, including .git
function create_public_repo () {
mkdir -p "$1"
pushd "$1" > /dev/null
git --bare init
popd > /dev/null
}
# Takes the complete path of the public repo, including .git
function push_to_public_repo () {
until git remote add $REMOTE "$1"; do
echo -n "Enter a new remote name: "
read REMOTE
done
git push $REMOTE $BRANCH
}
# Takes the complete path of the public repo, including .git
function enable_http_sharing () {
pushd "$1" >/dev/null
git --bare update-server-info
# chmod a+x hooks/post-update
popd > /dev/null
}
function clean () {
if [ was_unpublished ]; then
rm -rf "$public_dir"
fi
exit 1
}
get_top_level_dir
get_public_paths
trap clean INT
if [ -d "$public_dir" ]; then
if is_bare_repo "$public_dir"; then
echo "Already published in $public_dir"
echo "Public URL: $public_url"
else
echo "Directory $public_dir already exists but it's not a git bare repo"
fi
else
was_unpublished=1
create_public_repo $public_dir
push_to_public_repo $public_dir
enable_http_sharing $public_dir
echo "Public URL: $public_url"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment