| 1 | #!/usr/bin/env bash |
| 2 | # Points a checkout cloned from GitHub at this repository's home on Diffinery |
| 3 | # (diffinery.com/codician/diffinery). GitHub stopped receiving changes at the cut-off. |
| 4 | # |
| 5 | # scripts/use-diffinery.sh |
| 6 | # |
| 7 | # Needs DIFFINERY_TOKEN. Git reads it through a credential helper that looks at the |
| 8 | # environment, so the token is never written to disk or put on a command line. GitHub |
| 9 | # stays as a fetch-only remote named "github". When the checkout is clean, the current |
| 10 | # branch moves forward to Diffinery's copy of it, or to Diffinery's main if the branch |
| 11 | # is new and starts from GitHub's frozen main. |
| 12 | set -euo pipefail |
| 13 | [ -n "${DIFFINERY_TOKEN:-}" ] || { echo "DIFFINERY_TOKEN is not set" >&2; exit 1; } |
| 14 | url=${DIFFINERY_REPO_URL:-https://diffinery.com/codician/diffinery.git} |
| 15 | |
| 16 | git config --global credential.https://diffinery.com.helper \ |
| 17 | '!f() { [ "$1" = get ] && printf "username=x-token\npassword=%s\n" "$DIFFINERY_TOKEN"; }; f' |
| 18 | |
| 19 | if [ "$(git remote get-url origin 2>/dev/null || true)" != "$url" ]; then |
| 20 | if git remote get-url origin >/dev/null 2>&1 && ! git remote get-url github >/dev/null 2>&1; then git remote rename origin github; fi |
| 21 | if git remote get-url origin >/dev/null 2>&1; then git remote set-url origin "$url"; else git remote add origin "$url"; fi |
| 22 | fi |
| 23 | git remote get-url github >/dev/null 2>&1 && git remote set-url --push github nothing-goes-back-to-github |
| 24 | git fetch -q origin |
| 25 | git remote set-head origin -a >/dev/null 2>&1 || true |
| 26 | |
| 27 | branch=$(git symbolic-ref -q --short HEAD || true) |
| 28 | if [ -n "$branch" ] && [ -z "$(git status --porcelain)" ]; then |
| 29 | for base in "origin/$branch" origin/main; do |
| 30 | if git rev-parse -q --verify "$base" >/dev/null && git merge-base --is-ancestor HEAD "$base"; then |
| 31 | git merge -q --ff-only "$base" |
| 32 | echo "$branch is now at $base ($(git rev-parse --short HEAD))" |
| 33 | break |
| 34 | fi |
| 35 | done |
| 36 | if git rev-parse -q --verify "origin/$branch" >/dev/null; then git branch -q --set-upstream-to="origin/$branch"; fi |
| 37 | fi |
| 38 | echo "origin is $url. Push there; use https://diffinery.com/api/v3 with the same token for pull requests." |