Skip to content

Instantly share code, notes, and snippets.

@SiteRelEnby
Last active August 25, 2023 22:13
Show Gist options
  • Select an option

  • Save SiteRelEnby/444abfd56e91bfaa040e2b9fd5f726c5 to your computer and use it in GitHub Desktop.

Select an option

Save SiteRelEnby/444abfd56e91bfaa040e2b9fd5f726c5 to your computer and use it in GitHub Desktop.
anduril bzr -> git
#! /usr/bin/env bash
set -e -o pipefail
USE_DOCKER_FILTER_REPO=1
#[[ -z "${BRANCHES}" ]] && BRANCHES="anduril2 multi-channel"
[[ -z "${BRANCHES}" ]] && BRANCHES="multi-channel"
[[ -z "${LP_USER}" ]] && LP_USER="toykeeper"
# "auto": Delete commits that became empty due to filtering; preserve ones that were already empty. "always" / "never" = obvious
[[ -z "${PRUNE_EMPTY}" ]] && PRUNE_EMPTY="never"
if [[ ! -z "${TEMP_REPO_PATH}" ]]
then
tmpdir=${TEMP_REPO_PATH}
[[ ! -d $tmpdir ]] && mkdir -p $tmpdir
else
tmpdir=$(mktemp -p . -d)
fi
# Check if we have filter-repo already and get it if not
if which git-filter-repo > /dev/null
then
FILTER_REPO=`which git-filter-repo`
elif [[ -x ./git-filter-repo ]]
then
FILTER_REPO="$(pwd -P)/git-filter-repo"
else
(cd /tmp && wget https://github.com/newren/git-filter-repo/releases/download/v2.38.0/git-filter-repo-2.38.0.tar.xz \
&& tar -xJf /git-filter-repo-2.38.0.tar.xz && rm git-filter-repo-2.38.0.tar.xz ; exit $?)
cp /tmp/git-filter-repo-2.38.0/git-filter-repo /tmp
FILTER_REPO="/tmp/git-filter-repo"
fi
cd ${tmpdir}
for branch in ${BRANCHES}
do
echo $branch
# disabled pulling from LP because it's just as slow, seems to be the same operation, and adds complexity with filter-repo
# TODO: Put this in a cronjob that gets called when LP updates?
git clone "bzr::lp:~${LP_USER}/flashlight-firmware/${branch}" 2>/dev/null
cd "${branch}"
if git init --initial-branch="${branch}" 2>/dev/null
then
continue
else
#old git version
git init
git checkout -b "${branch}"
git branch -D master || git branch -D main #is there a version with main and no --initial-branch ?
fi
# First, enumerate a list of files we do want
git log --pretty=format: --name-only | sort -u | grep -E -e ^bin/ -e ^ToyKeeper \
| grep -v -e '^ToyKeeper/STAR_1mode/' -e '^ToyKeeper/STAR_noinit/' -e '^ToyKeeper/uv/' -e 'ToyKeeper/starry-offtime/' \
-e '^ToyKeeper/RoundTable/' -e '^ToyKeeper/s7/' -e '^ToyKeeper/Ferrero_Rocher/' -e '^ToyKeeper/DarkHorse/' \
-e '^ToyKeeper/battcheck/' -e '^ToyKeeper/bistro/' -e '^ToyKeeper/blf-a6/' -e '^ToyKeeper/crescendo/' \
-e '^ToyKeeper/DarkHorse/' -e '^ToyKeeper/Ferrero_Rocher/' -e '^ToyKeeper/STAR_SRK/' -e '^ToyKeeper/tail-light/' \
-e '^ToyKeeper/cypreus/' -e '^ToyKeeper/spaghetti-monster/momentary/' -e '^ToyKeeper/spaghetti-monster/fireflies-ui/' \
-e '^ToyKeeper/spaghetti-monster/ramping-ui/' -e '^ToyKeeper/spaghetti-monster/baton/' \
-e '^ToyKeeper/spaghetti-monster/darkhorse/' -e '^ToyKeeper/spaghetti-monster/meteor/' \
-e '^ToyKeeper/spaghetti-monster/werner/' -e '^ToyKeeper/spaghetti-monster/rampingios/' > files_keep.txt
# Then handle renaming files
git log --pretty=format: --name-only | grep -e '^ToyKeeper' > ./files2.txt
cat files2.txt | sort | uniq > files3.txt
while read line; do new=$(echo $line | sed 's|^ToyKeeper/||') ; echo "${line}==>${new}"; done < files3.txt > files_rename.txt
rm files2.txt files3.txt
# my "miscellaneous hacking box" has a too old version of git to use filter-repo and I was being lazy:
if [[ ! -z "${USE_DOCKER_FILTER_REPO}" ]]
then
if [[ ! -f ./git-filter-repo ]]
then
cp ${FILTER_REPO} ./
fi
sudo docker run -v $PWD:/mnt --rm -it python:3 sh -c "cd /mnt ; git config --global --add safe.directory /mnt ; \
./git-filter-repo --prune-empty=${PRUNE_EMPTY} --paths-from-file files_keep.txt --force" && \
sudo chown -R $(whoami):$(groups $(whoami) | awk '{print $3}') ./
sudo docker run -v $PWD:/mnt --rm -it python:3 sh -c "cd /mnt ; git config --global --add safe.director /mnt ; \
./git-filter-repo --prune-empty=${PRUNE_EMPTY} --paths-from-file files_rename.txt --force" && \
sudo chown -R $(whoami):$(groups $(whoami) | awk '{print $3}') ./
else
# If you have a newer version:
${FILTER_REPO} --prune-empty=${PRUNE_EMPTY} --paths-from-file files_keep.txt --force
${FILTER_REPO} --prune-empty=${PRUNE_EMPTY} --paths-from-file files_rename.txt --force
fi
sed -i 's|\.\./\.\./\.\./bin/build.sh|../../bin/build.sh|' spaghetti-monster/anduril/build-all.sh
# PLACEHOLDER: set up git name/email here if desired
git commit -m 'update for restructured repo' spaghetti-monster/anduril/build-all.sh
done
echo ${tmpdir}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment