From de48aad638e4ec5afccde5d2d2d3c9ecb7d959eb Mon Sep 17 00:00:00 2001 From: Merlin Mathesius Date: Jun 22 2020 15:11:43 +0000 Subject: Add shell script that can be used to remove old branches from the merged configuration repos Signed-off-by: Merlin Mathesius --- diff --git a/contrib/scripts/delete-old-branches.sh b/contrib/scripts/delete-old-branches.sh new file mode 100755 index 0000000..e5e7de4 --- /dev/null +++ b/contrib/scripts/delete-old-branches.sh @@ -0,0 +1,81 @@ +#!/bin/bash + +usage() { + cat >&2 <<-EOF +Usage: $0 [ --dry-run ] age repo [ id_file ] + +Removes branches older than 'age' seconds from git repo 'repo'. + +If optional 'id_file' is specified, it is used as the private key file +for authenticating to git. +EOF + exit 1 +} + +dry_run=0 + +while (( $# >= 1 )) +do + case "$1" in + --dry-run ) + dry_run=1 + shift + ;; + -* ) + echo "Unknown option: $1" >&2 + usage + ;; + * ) + break + ;; + esac +done + +if (( $# < 2 || $# > 3 )) +then + usage +fi + +prune_age="$1" +repo="$2" +id_file="${3:-}" + +echo "Removing branches older than $prune_age seconds from repo $repo" + +if [ -n "$id_file" ] +then + export GIT_SSH_COMMAND="ssh -o IdentitiesOnly=yes -i $id_file" +fi + +e_now=$EPOCHSECONDS + +workdir=`mktemp -d` +git clone "$repo" "$workdir" +pushd "$workdir" >/dev/null + +branches=$(git branch -r --no-merged) +for branch in $branches +do + br_name="${branch#origin/}" + if [ "$br_name" == master ] + then + next + fi + # get epoch time of last commit in branch + e_last=$(git log -n 1 --format="%ct" $branch) + age=$(( e_now - e_last )) + echo "Last commit to branch $branch was $age seconds ago" + if (( $age > $prune_age )) + then + echo "DELETING BRANCH $branch" + if (( $dry_run )) + then + echo DRY RUN NOT RUNNING git push origin --delete $br_name + else + git push origin --delete $br_name + fi + fi +done + +popd >/dev/null +rm -rf "$workdir"