Skip to content

Instantly share code, notes, and snippets.

@justmiles
Created June 3, 2024 16:18
Show Gist options
  • Save justmiles/dbad304cb2a57d710d85d42872bbedca to your computer and use it in GitHub Desktop.
Save justmiles/dbad304cb2a57d710d85d42872bbedca to your computer and use it in GitHub Desktop.
#!/bin/bash
set -ex
# nix-shell -p git-filter-repo moreutils gh
SOURCE_PROVIDER=$1
GH_REPO_OWNER=justmiles
git clone git@github.com:cloudquery/cloudquery.git "cq-source-${SOURCE_PROVIDER}"
cd "cq-source-${SOURCE_PROVIDER}"
# Get commit prior to removal (skip the actual removal commit & the docs removal commit)
COMMIT=$(git log --full-history --pretty=format:"%h" --skip 2 -- "plugins/source/${SOURCE_PROVIDER}/README.md" | head -1)
# Delete commits since legacy switch
GIT_SEQUENCE_EDITOR="sed -ri 's/pick/d/'" git rebase -i $COMMIT
sleep 5
# identify irrellevent files
find . -type f \
| grep -v "^./plugins/source/${SOURCE_PROVIDER}/" \
| grep -v "^./LICENSE" \
| grep -v "^./.git/" | sed 's@^./@@' > .git/irrellevent-files.txt
# delete irrelevent files
cat .git/irrellevent-files.txt | xargs -I % rm -rf "%"
# delete empty directories
find . -type d -empty -delete
# Remove unused files from history
git filter-repo --force --analyze
tail +3 .git/filter-repo/analysis/path-deleted-sizes.txt \
| tr -s ' ' \
| cut -d ' ' -f 5- \
| sort > .git/filter-repo/analysis/path-deleted.txt
cat .git/filter-repo/analysis/path-deleted.txt .git/irrellevent-files.txt | sort | uniq > .git/filter-repo/analysis/combined-path-deleted.txt
find . -type f | sed 's#^./##' | sort > .git/existing-files.text
comm -13 .git/existing-files.text .git/filter-repo/analysis/combined-path-deleted.txt > .git/paths-to-delete.txt
git filter-repo --force --prune-empty always --prune-degenerate always --replace-refs delete-and-add --invert-paths --paths-from-file .git/paths-to-delete.txt
# Delete extra branches
git branch | grep -v "main$" | xargs git branch -D
git mv plugins/source/${SOURCE_PROVIDER}/* .
cat << EOF | cat - README.md | sponge README.md
> NOTICE: This repository is the restoration of CloudQuery's legacy open source provider \`cq-source-${SOURCE_PROVIDER}\`
> following CloudQuery's [movement of previously open-source plugins to closed-source](https://www.cloudquery.io/blog/cloudquery-official-free-plugins-moving-to-paid).
> For the latest and greatest these plugins have to offer subscribe to the official plugins from the CloudQuery hub. This repo will accept pull requests but generally only exists
> to maintain security updates for the previously open-source plugin.
---
EOF
# fix the tags
# remove base version tags
git tag | grep "^v" | xargs git tag -d
# strip the monorepo tag prefix
git tag | grep "plugins/source/${SOURCE_PROVIDER}" | while read tag; do
new_tag=$(basename $tag)
git tag $new_tag $tag:
git tag -d $tag
done
# delete tags associated with projects in monorepo
git tag | grep -v "^v" | xargs git tag -d
git commit -m "refactor: restore legacy cq-source-${SOURCE_PROVIDER} from cloudquery/cloudquery@${COMMIT}"
# Delete remote branches
# git branch -r | grep -v "origin/main" | sed 's@origin/@@' | xargs git push origin --delete
# Create github
gh repo create "cq-source-${SOURCE_PROVIDER}" --public
git remote add origin "git@github.com:${GH_REPO_OWNER}/cq-source-${SOURCE_PROVIDER}.git"
# push the changes
git push -u origin --all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment