Created
January 10, 2019 11:50
-
-
Save hoegaarden/67aa50eeea7bf015dcfc7a51ab083d15 to your computer and use it in GitHub Desktop.
Script to update / verify `repo-infra` when `subtree`ed into other repos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eu | |
set -o pipefail | |
readonly upstreamURL="${REPO_INFRA_URL:-git@github.com:kubernetes/repo-infra}" | |
readonly tempName="temp-repo-infra-${RANDOM}" | |
readonly repoRoot="$( git rev-parse --show-toplevel )" | |
readonly targetPath="${REPO_INFRA_PATH:-repo-infra}" | |
quietGit() { | |
local stdout stderr rc | |
stdout="$(mktemp)" | |
stderr="$(mktemp)" | |
set +e | |
git "$@" </dev/null >"$stdout" 2>"$stderr" | |
rc=$? | |
set -e | |
[ $rc -ne 0 ] && { | |
cat "$stdout" | |
cat "$stderr" >&2 | |
} | |
rm -f -- "$stdout" "$stderr" | |
return $rc | |
} | |
addTempRemote() { | |
quietGit remote add "$tempName" "$upstreamURL" | |
quietGit fetch "$tempName" | |
} | |
forkTempBranch() { | |
local oldBranch | |
oldBranch="$( git rev-parse --abbrev-ref HEAD )" | |
quietGit checkout -b "$tempName" | |
echo "$oldBranch" | |
} | |
delTempRemote() { | |
quietGit remote remove "$tempName" | |
} | |
resetForkedBranch() { | |
local oldBranch="$1" | |
quietGit reset | |
quietGit checkout "$oldBranch" | |
quietGit branch -D "$tempName" | |
} | |
updateRepoInfra() { | |
quietGit subtree pull --prefix "$targetPath" "$tempName" master --squash | |
} | |
update() { | |
cd "$repoRoot" | |
trap delTempRemote EXIT | |
addTempRemote | |
updateRepoInfra | |
} | |
verify() { | |
local orgRev newRev | |
cd "$repoRoot" | |
addTempRemote | |
trap delTempRemote EXIT | |
oldBranch="$( forkTempBranch )" | |
trap 'resetForkedBranch "$oldBranch"; delTempRemote' EXIT | |
orgRev="$( git rev-parse HEAD )" | |
updateRepoInfra | |
newRev="$( git rev-parse HEAD )" | |
[ "$orgRev" != "$newRev" ] && { | |
{ | |
echo "${targetPath} out of sync with ${upstreamURL}" | |
echo | |
git diff --stat "$orgRev" | |
} >&2 | |
return 1 | |
} | |
} | |
main() { | |
case "$0" in | |
*update*) update ;; | |
*verify*) verify ;; | |
*) echo 'not allowed' >&2 ; return 254 ;; | |
esac | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment