Skip to content

Commit add3a9a

Browse files
committed
feat: add update_refs.sh script
1 parent 8acb9be commit add3a9a

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

_scripts/update_refs.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
5+
6+
# Ensure we are not on the `main` branch.
7+
if [[ "$CURRENT_BRANCH" == "main" ]]; then
8+
>&2 echo "Will not replace github references for the main branch. Exiting."
9+
exit 1
10+
fi
11+
12+
# Ensure the index is clean
13+
if ! git diff-index --quiet HEAD --; then
14+
>&2 echo "Dirty git index. Check working tree or staged changes. Exiting."
15+
exit 2
16+
fi
17+
18+
# Warn if the branch isn't a `release-*` branch (these changes shouldn't be in a branch that gets merged to main)
19+
if [[ "$CURRENT_BRANCH" != release-* ]]; then
20+
>&2 echo "WARNING: doesn't look like a release branch. Continuing anyway."
21+
fi
22+
23+
# Search for githubusercontent urls and replace the branch reference with a placeholder variable
24+
# This is done just in case the branch has special regex characters (like `/`).
25+
# shellcheck disable=SC2016 # We intentionally don't want to expand the variable.
26+
find demos stacks -type f \
27+
-exec grep -l githubusercontent {} \; \
28+
-exec sed -i -E 's/(stackabletech\/demos)\/main\//\1\/\${UPDATE_BRANCH_REF}\//' {} \;
29+
30+
# Now, for all modified files, we can use envsubst
31+
export UPDATE_BRANCH_REF="$CURRENT_BRANCH"
32+
for MODIFIED_FILE in $(git diff --name-only); do
33+
# shellcheck disable=SC2016 # We intentionally don't want to expand the variable.
34+
envsubst '$UPDATE_BRANCH_REF' < "$MODIFIED_FILE" > "$MODIFIED_FILE.replacements"
35+
mv "$MODIFIED_FILE.replacements" "$MODIFIED_FILE"
36+
done

0 commit comments

Comments
 (0)