Diffinery

codician/diffinerypublic

Open-source, agent-first GitHub and GitHub Actions alternative that runs on your own Cloudflare account: git hosting, pull requests, code review and drop-in GitHub Actions YAML on Workers, Durable Objects, R2 and Containers.

48 lines · 2.6 KB · executableRaw History
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.
21set -euo pipefail
22[ -n "${DIFFINERY_TOKEN:-}" ] || { echo "DIFFINERY_TOKEN is not set" >&2; exit 1; }
23url=${DIFFINERY_REPO_URL:-https://diffinery.com/codician/diffinery.git}
24
25host=${url#*://}; host=${host%%/*}
26git config --global "credential.https://$host.helper" \
27 '!f() { [ "$1" = get ] && printf "username=x-token\npassword=%s\n" "$DIFFINERY_TOKEN"; }; f'
28
29if [ "$(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
32fi
33git remote get-url github >/dev/null 2>&1 && git remote set-url --push github nothing-goes-back-to-github
34git fetch -q origin
35git remote set-head origin -a >/dev/null 2>&1 || true
36
37branch=$(git symbolic-ref -q --short HEAD || true)
38if [ -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
47fi
48echo "origin is $url. Push there; open pull requests on https://$host, or through https://$host/api/v3 with the same token."