Skip to content

Instantly share code, notes, and snippets.

@slyfer
Forked from mattalxndr/git-svn-prune.sh
Created October 17, 2017 10:21
Show Gist options
  • Save slyfer/7f612eefc66bfc7956d9ee3ef319c158 to your computer and use it in GitHub Desktop.
Save slyfer/7f612eefc66bfc7956d9ee3ef319c158 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#set -x
# Delete references to SVN branches that no longer exist
# Usage (this will not execute without asking you to confirm the list):
# bin/git-svn-prune.sh
# Started with
# Set your SVN prefix
if [ $# == 1 ]
then
GIT_SVN_PREFIX=$1
else
GIT_SVN_PREFIX="svn/"
fi
SVN_REPO=$(git config --get svn-remote.svn.url)
GIT_SVN_PREFIX_SED=$(echo "$GIT_SVN_PREFIX" | sed -e 's/[\/&]/\\&/g')
GIT_SVN_BRANCHES=$( git config --get svn-remote.svn.branches | cut -d '*' -f 1)
SVN_BRANCHES_URL="$SVN_REPO"/"$GIT_SVN_BRANCHES"
TEMP_DIR=$(mktemp -d)
SVN_BRANCHES_FILE="$TEMP_DIR/svn-branches"
GIT_BRANCHES_FILE="$TEMP_DIR/git-branches"
OLD_BRANCHES_FILE="$TEMP_DIR/old-branches"
svn ls "$SVN_BRANCHES_URL" | sed 's|^[[:space:]]*||' | sed 's|/$||' > "$SVN_BRANCHES_FILE"
if [[ ! -s "$SVN_BRANCHES_FILE" ]]
then
echo "No remote SVN branches found at \"$SVN_BRANCHES_URL\". Check configuration."
exit 1
fi
git branch -r | sed 's|^[[:space:]]*||' > "$GIT_BRANCHES_FILE"
if [[ $GIT_SVN_PREFIX ]]
then
sed -i -e 's/^'"$GIT_SVN_PREFIX_SED"'//' "$GIT_BRANCHES_FILE"
fi
sed -i -e '/^tags/d;/\//d;/trunk/d' "$GIT_BRANCHES_FILE"
#echo "bad-branch" >> "$GIT_BRANCHES_FILE"
#echo "another-bad-branch" >> "$GIT_BRANCHES_FILE"
if [[ ! -s "$GIT_BRANCHES_FILE" ]]
then
echo "Your local git repository contains no references to any SVN branches at all, deleted or not. Check configuration."
exit 1
fi
diff -u "$GIT_BRANCHES_FILE" "$SVN_BRANCHES_FILE" | grep '^-' | sed 's|^-||' | grep -v '^--' > "$OLD_BRANCHES_FILE"
if [[ $GIT_SVN_PREFIX ]]
then
sed -i -e 's/^/'"$GIT_SVN_PREFIX_SED"'/' "$OLD_BRANCHES_FILE"
fi
if [[ -s "$OLD_BRANCHES_FILE" ]]
then
echo "References to deleted SVN branches were found in your local git repository:"
echo
cat "$OLD_BRANCHES_FILE" | sed 's/^/ - /'
echo
read -p 'Delete the references from local git respository? Press "y" or "n" ' -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo
for BRANCH in `cat "$OLD_BRANCHES_FILE"`
do
echo "Deleting $BRANCH ..."
# UNCOMMENT THE FOLLOWING TWO LINES TO ENABLE SCRIPT
#git branch -d -r "$BRANCH"
done
GREEN='\033[0;32m'
NC='\033[0m'
echo
echo -e "${GREEN}Note: This was only a test. This script is not stable enough yet to actually do anything on its own.${NC}"
echo -e "${GREEN} If the list of deleted branches was accurate, then open the script up in an editor,${NC}"
echo -e "${GREEN} uncomment the lines that actually delete the branches, then execute the script again.${NC}"
fi
else
echo "Your local git repository contains no references to deleted SVN branches."
fi ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment