| 1 | #!/usr/bin/env bash |
| 2 | # Points a checkout cloned from the GitHub mirror (github.com/codician-team/diffinery) at |
| 3 | # the project's home on Diffinery (https://diffinery.com/codician/diffinery), where |
| 4 | # branches are pushed and pull requests are opened. The mirror follows Diffinery, so |
| 5 | # changes go to Diffinery first. |
| 6 | # |
| 7 | # DIFFINERY_TOKEN=dfy_... scripts/use-diffinery.sh |
| 8 | # |
| 9 | # DIFFINERY_TOKEN is a Diffinery token with the `write` scope (Settings -> Tokens). |
| 10 | # DIFFINERY_REPO_URL points the checkout at another copy, such as your fork or your own |
| 11 | # instance (default https://diffinery.com/codician/diffinery.git). |
| 12 | # |
| 13 | # What it changes: |
| 14 | # - Adds a git credential helper for the Diffinery host to your global git config. The |
| 15 | # helper reads DIFFINERY_TOKEN from the environment when git asks, so the token is never |
| 16 | # written to disk or put on a command line. |
| 17 | # - Renames the GitHub remote to "github", fetch-only (pushes there would be overwritten |
| 18 | # by the mirror), and makes "origin" the Diffinery repository. |
| 19 | # - When the checkout is clean, moves the current branch forward to Diffinery's copy of |
| 20 | # it, or to Diffinery's main if the branch is new and starts from the mirror's main. |
| 21 | set -euo pipefail |
| 22 | [ -n "${DIFFINERY_TOKEN:-}" ] || { echo "DIFFINERY_TOKEN is not set" >&2; exit 1; } |
| 23 | url=${DIFFINERY_REPO_URL:-https://diffinery.com/codician/diffinery.git} |
| 24 | |
| 25 | host=${url#*://}; host=${host%%/*} |
| 26 | git config --global "credential.https://$host.helper" \ |
| 27 | '!f() { [ "$1" = get ] && printf "username=x-token\npassword=%s\n" "$DIFFINERY_TOKEN"; }; f' |
| 28 | |
| 29 | if [ "$(git remote get-url origin 2>/dev/null || true)" != "$url" ]; then |
| 30 | 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 |
| 31 | if git remote get-url origin >/dev/null 2>&1; then git remote set-url origin "$url"; else git remote add origin "$url"; fi |
| 32 | fi |
| 33 | git remote get-url github >/dev/null 2>&1 && git remote set-url --push github nothing-goes-back-to-github |
| 34 | git fetch -q origin |
| 35 | git remote set-head origin -a >/dev/null 2>&1 || true |
| 36 | |
| 37 | branch=$(git symbolic-ref -q --short HEAD || true) |
| 38 | if [ -n "$branch" ] && [ -z "$(git status --porcelain)" ]; then |
| 39 | for base in "origin/$branch" origin/main; do |
| 40 | if git rev-parse -q --verify "$base" >/dev/null && git merge-base --is-ancestor HEAD "$base"; then |
| 41 | git merge -q --ff-only "$base" |
| 42 | echo "$branch is now at $base ($(git rev-parse --short HEAD))" |
| 43 | break |
| 44 | fi |
| 45 | done |
| 46 | if git rev-parse -q --verify "origin/$branch" >/dev/null; then git branch -q --set-upstream-to="origin/$branch"; fi |
| 47 | fi |
| 48 | echo "origin is $url. Push there; open pull requests on https://$host, or through https://$host/api/v3 with the same token." |