#!/bin/bash

set -exu

git clone https://github.com/libguestfs/libguestfs virt-p2v
cd virt-p2v

# virt-p2v was introduced after 1.27.9, so remove all the tags before that:
git tag -l | grep -v '^v' | sort -r -V | sed -e '0,/1\.27\.10/d' | xargs git tag -d
git filter-branch --subdirectory-filter p2v --prune-empty --tag-name-filter cat -- --all
rm -rf .git/refs/original/

# then remove all the branches without p2v:
# - stable branches
for i in $(seq 28 2 40); do
  git branch -t stable-1.$i origin/stable-1.$i
done
# - rhel branches
for b in $(git for-each-ref --format='%(refname:lstrip=-1)' refs/remotes/origin | grep rhel-7.[1-7]); do
  git branch -t $b origin/$b
done
# - extra branches
for b in fedora-26; do
  git branch -t $b origin/$b
done
git remote remove origin

git reflog expire --all --verbose --stale-fix --expire=now --expire-unreachable=now
git gc --aggressive --prune=now
