Skip to content

Instantly share code, notes, and snippets.

@danixland
Last active July 12, 2018 08:28
Show Gist options
  • Save danixland/69ce5b6eddc8cad2944194623e355e83 to your computer and use it in GitHub Desktop.
Save danixland/69ce5b6eddc8cad2944194623e355e83 to your computer and use it in GitHub Desktop.
script to delete a bare git repository after checking some conditions. Save it inside the git-shell-commands directory as "delete"
#! /bin/bash
# usage: delete <REPOSITORY> - PERMANENTLY delete a repository if existing.
# CAREFUL, this action cannot be undone. This command will ask for confirmation.
GITDIR="/var/git"
function is_bare() {
repodir=$1
if "$(git --git-dir="$repodir" rev-parse --is-bare-repository)" = true
then
true
else
false
fi
}
if [ ! -z $1 ]; then
PROJECT=$1
else
read -p 'Project to delete: ' PROJECT
fi
if [ -d ${GITDIR}/${PROJECT}.git ]; then
if [[ $(ls -A ${GITDIR}/${PROJECT}.git) ]]; then
if is_bare ${GITDIR}/${PROJECT}.git
then
echo "You are going to delete the git repository \"${PROJECT}.git\" Do you really want to continue? Note, this action cannot be reverted. [y/N]"
read delAnswer
case $delAnswer in
Y|y)
rm -rf ${PROJECT}.git
;;
N|n)
echo "Aborting due to user request."
exit 173
;;
*)
echo "you said \"$delAnswer\" which I don't understand. Assuming No. Aborting."
exit 177
;;
esac
else
echo "\"${PROJECT}.git\" doesn't look like a git repository. Check with your System Administrator."
exit 177
fi
else
echo "\"${PROJECT}.git\" is an empty directory, Skipping. Check with your System Administrator."
exit 177
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment