Skip to content

Instantly share code, notes, and snippets.

@akhilerm
Created October 4, 2022 12:33
Show Gist options
  • Save akhilerm/330a603ec49e4c6cc51c722ec15f4e82 to your computer and use it in GitHub Desktop.
Save akhilerm/330a603ec49e4c6cc51c722ec15f4e82 to your computer and use it in GitHub Desktop.
Script to extract api from containerd whenever a new tag is pushed to containerd/containerd
#!/usr/bin/env bash
# Usage
# api-split.sh https://github.com/akhilerm/containerd-api https://github.com/akhilerm/containerd release/1.6 v1.6.10
# the tag should exist in the source repo, also source branch should exist
API_REPO=$1
#API_REPO="https://github.com/akhilerm/containerd-api"
SOURCE_REPO=$2
#SOURCE_REPO="https://github.com/akhilerm/containerd"
# the branch from where we fetch the changes and push should be same in both the repos
# API_BRANCH and SOURCE_BRANCH should be equal.
API_BRANCH=$3
#API_BRANCH="main"# the branch from where we fetch the changes and push should be same in both the repos
## API_BRANCH and SOURCE_BRANCH should be equal.
#API_BRANCH=$2
##API_BRANCH="main"
SOURCE_BRANCH="${API_BRANCH}"
#SOURCE_BRANCH="main"
TAG=$4
WORK_REPO_DIRECTORY=api-repo-testing
WORK_BRANCH="api-for${TAG}"
# clone the api repo
git clone "${API_REPO}" "${WORK_REPO_DIRECTORY}"
cd "${WORK_REPO_DIRECTORY}" || exit 1
# get latest tag available in the api repo
git checkout "${SOURCE_BRANCH}"
LATEST_TAG_IN_API=$(git describe --tags --abbrev=0)
# COMMIT_OF_LATEST_TAG_IN_SOURCE=git show-ref -s ${LATEST_TAG_IN_API}
# delete all the tags locally so that it does nt collide with the tags that we will be fetching from source repo
git tag -l | xargs -L1 git tag --delete
# get the newly pushed tag from upstream
git remote add upstream "${SOURCE_REPO}"
git fetch --all
# checkout to the target tag
git checkout "tags/${TAG}"
# create a new branch for performing filtering on the target tag
git checkout -b "${WORK_BRANCH}"
# filter all these directories
git filter-repo --force \
--path api/ \
--path go.mod \
--path go.sum \
--path platforms/ \
--path errdefs/ \
--path protobuf/
COMMIT_OF_PREVIOUS_TAG_IN_SOURCE=$(git show-ref -s "{LATEST_TAG_IN_API}")
# Patch the go.mod
sed -i -e '/replace (/,/)/d' go.mod
rm go.mod-e
go mod tidy
git add go.mod go.sum
git commit -sS -m "updating go modules for ${TAG}"
# after running filter repo, all our remotes will have gone, so we add the remote again
git remote add origin "${API_REPO}"
git fetch origin "${API_BRANCH}" # if this API_BRANCH does not exist, it means the new tag was pushed in a new release branch
# so we should push our ${WORK_BRANCH} as the new API_BRANCH into API_REPO and then tag and push
# now we checkout to the resulting branch where we need to rebase the api changes into
git checkout "${API_BRANCH}"
# rebase the changes from our working(filtered) branch to release branch
# 1. Either we can do rebase
# git rebase "${WORK_BRANCH}"
# 2. Or, cherry pick all commits from $COMMIT_OF_PREVIOUS_TAG_IN_SOURCE to current HEAD into API_BRANCH
git push origin "${API_BRANCH}"
# delete all the tags again, because the tag we are going to create already exists
git tag -l | xargs -L1 git tag --delete
# tag and push
git tag "${TAG}"
git push origin "${TAG}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment