Commit a2e0f227 authored by intrigeri's avatar intrigeri
Browse files

Replace git-delete-branch with a new Python 3 implementation.

parent f848aafc
#!/usr/bin/python3
import argparse
import pprint
import sys
from distutils.util import strtobool
from tailslib.git import Git
### Functions
def yes_no_input(prompt, default=True):
if default:
options = 'Y/n'
else:
options = 'N/y'
sys.stdout.write('%s [%s] ' % (prompt, options))
while True:
answer = input().lower()
if len(answer) == 0:
return default
else:
try:
return strtobool(answer)
except ValueError:
print("Please respond with 'y' or 'n'.")
### Parse command-line arguments
parser = argparse.ArgumentParser(description='Delete merged Git branches.')
parser.add_argument('--repo', type=str, dest='repo',
default='boum_org_amnesia@webmasters.boum.org:wiki.git',
help='Path to an up-to-date (bare) Tails Git repository.')
parser.add_argument('--remote', type=str, dest='remote', default='origin',
help='Push to the specified remote instead of "origin".')
parser.add_argument('--batch', type=bool, dest='batch',
nargs='?', const=True, default=False,
help='Assume "yes" as answer to all prompts.')
args = parser.parse_args()
### Main
pp = pprint.PrettyPrinter()
tailsgit = Git(args.repo)
branches_to_delete = tailsgit.branches_to_delete()
if not branches_to_delete:
print("No branch to delete was found.")
sys.exit(0)
print("The following branches will be deleted:")
pp.pprint(branches_to_delete)
if not args.batch and not yes_no_input("Remove these branches?", default=False):
sys.exit(0)
tailsgit.push(args.remote, [':%s' % branch for branch in branches_to_delete])
#!/bin/sh
#############################
# Configure as needed #
#############################
# Set to 1 to remove merged branches from remotes
REMOVE_FROM_REMOTE=1
# If REMOVE_FROM_REMOTE was set to 1 above, set this to the remote
# where branches shall be removed
MANAGED_REMOTE="origin"
# These branches will never be removed
BRANCHES_TO_KEEP="head master stable testing devel experimental feature/8617-delete-obsolete-Git-branches"
######################################################
# Nothing below this point should need to be edited #
######################################################
generate() {
# Take in space delimited string, output | delimited
echo "$1" | sed 's/\s/|/g'
}
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "${PWD} is not a Git tree. Exiting."
exit 1
fi
CURRENT=$(git rev-parse --abbrev-ref HEAD)
if [ "$CURRENT" != 'master' ] && \
[ "$CURRENT" != 'feature/8617-delete-obsolete-Git-branches' ] ; then
echo "Switch to the master branch before running this script." >&2
exit 1
fi
echo "Fetching from remote ${MANAGED_REMOTE}..."
git fetch --prune "$MANAGED_REMOTE"
[ $REMOVE_FROM_REMOTE -eq 1 ] && \
REMOTE_BR=$(git branch -r --merged | grep -v '\->' |\
grep -vE "^\s+([^/]+)/($(generate "$BRANCHES_TO_KEEP"))$" |\
grep -E "^\s+$MANAGED_REMOTE/")
LOCAL_BR=$(git branch --merged | grep -Ev "^\*?\s+($(generate "$BRANCHES_TO_KEEP"))$")
if [ -z "$REMOTE_BR" ] && [ -z "$LOCAL_BR" ]; then
echo "Woohoo! No unmerged branches!" >&2
else
[ -n "$REMOTE_BR" ] && \
echo "The following merged remote branches will be removed:" && \
echo "$REMOTE_BR"
[ -n "$LOCAL_BR" ] && \
echo "The following merged local branches will be removed:" && \
echo "$LOCAL_BR"
echo -n "Remove these branches? (y/N): "
read answer
case $answer in
y|Y|Yes|yes)
REFS=''
for BRANCH in $REMOTE_BR; do
echo -n "Remove branch '$BRANCH'? (Y/n): "
read answer
case "$answer" in
''|y|Y|Yes|yes)
REFS="$REFS $(echo $BRANCH | sed 's/^[^/]\+\/\(.\+\)/:\1/')"
;;
esac
done
echo "$REFS" | xargs -n30 git push "$MANAGED_REMOTE"
if [ -n "$LOCAL_BR" ]; then
git branch --merged | grep -Ev "^\*?\s+($(generate "$BRANCHES_TO_KEEP"))$" | xargs -n30 git branch -d
fi
;;
*)
echo "Aborting due to user request." >&2
exit 0
esac
fi
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment