Skip to content

Instantly share code, notes, and snippets.

@njhale
Created November 30, 2021 14:56
Show Gist options
  • Save njhale/093c7b06c9efbaacb328860a7cee4ad9 to your computer and use it in GitHub Desktop.
Save njhale/093c7b06c9efbaacb328860a7cee4ad9 to your computer and use it in GitHub Desktop.
OLM Downstream Sync
#!/bin/bash
set -o errexit
set -o pipefail
num_commits=256
set +u
while getopts 'n:' flag; do
case "${flag}" in
n) num_commits=${optarg}; shift ;;
\?) exit 1 ;;
*) echo "unexpected option ${flag}"; exit 1 ;;
esac
shift
done
set -u
remote="${1:-api}"
branch="${2:-master}"
# slurp command output by newline, creating an array
mapfile -t remote_commits < <(git rev-list --topo-order --no-merges -n "${num_commits}" "${remote}/${branch}" | tac)
picked=0
cherrypick_set="${remote}.cherrypick"
: > "${cherrypick_set}" # clear existing file
for rc in "${remote_commits[@]}"; do
if [[ -z $(git log -n 1 --no-merges --grep "${rc}" HEAD) ]]; then
printf '%s\n' "${rc}" >> "${cherrypick_set}"
(( ++picked ))
fi
done
printf '%d cherry-pick candidate(s) written to %s\n' "${picked}" "${cherrypick_set}"
printf 'run "pop_candidate.sh -a %s" to cherry-pick all\n' "${remote}"
#!/bin/bash
cph=$(git rev-list -n 1 CHERRY_PICK_HEAD 2> /dev/null)
set -o errexit
set -o pipefail
pop_all=true
set +u
while getopts 'a:' flag; do
case "${flag}" in
a) pop_all=true; shift ;;
\?) exit 1 ;;
*) echo "unexpected option ${flag}"; exit 1 ;;
esac
shift
done
set -u
remote="${1:-api}"
subtree_dir="staging/${remote}"
cherrypick_set="${remote}.cherrypick"
remaining=$(wc -l < "${cherrypick_set}")
function pop() {
rc=$(head -n 1 "${cherrypick_set}")
if [[ ! $rc ]]; then
printf 'nothing to pick'
exit
fi
printf 'popping: %s\n' "${rc}"
if [[ ! $cph ]]; then
git cherry-pick --keep-redundant-commits -Xsubtree="${subtree_dir}" "${rc}"
else
if [[ $cph != "${rc}" ]]; then
printf 'unexpected CHERRY_PICK_HEAD:\ngot %s\nexpected: %s\n' "${cph}" "${rc}"
exit
fi
printf 'cherry-pick in progress for %s\n' "${cph}"
git add .
git cherry-pick --continue
fi
# 1. Pop next commit off cherrypick set
# 2. Cherry-pick
# 3. Ammend commit
# 4. Remove from cherrypick set
make vendor
make manifests
git add .
git status
git commit --amend --allow-empty --no-edit --trailer "Upstream-repository:${remote}" --trailer "Upstream-commit: ${rc}"
tmp_set=$(mktemp)
tail -n +2 "${cherrypick_set}" > "${tmp_set}"; cat "${tmp_set}" > "${cherrypick_set}"
(( --remaining ))
printf '%d picks remaining\n' "${remaining}"
if [[ $pop_all == 'true' ]] && (( remaining > 0 )); then
pop
fi
}
pop
@timflannagan
Copy link

A couple of notes while poking around this locally:

  • It looks like the remote=${1:-api} isn't respecting $1 explicitly overriding that value (e.g. using operator-registry as the remote)
  • I wasn't able to get the -n count working properly and had to adjust the num_count manually to avoid backtracking to the beginning of the commit history for some of the more shallow repositories
  • The git commit --trailer ... option appears to be added in git 2.32? I'm running 2.31 locally and had to upgrade my git version manually

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment