Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
scripts: Add `rad-{alternate,wire,worktree}`
Archived lorenz opened 1 year ago

Got a request to “put rad-wire […] in radicle” from stellarmagnet, and decided to also propose the other two. They all pass shellcheck.

3 files changed +103 -0 3403a66d 81ac53ca
added scripts/rad-alternate
@@ -0,0 +1,38 @@
+
#! /bin/sh
+
set -eu
+

+
diff -V > /dev/null
+
grep -V > /dev/null
+

+
REMOTE="rad"
+
PREFIX="rad://"
+
ALTERNATES="$(git rev-parse --absolute-git-dir)/objects/info/alternates"
+
URI=$(git remote get-url "$REMOTE")
+

+
if [ "${URI:0:6}" != "$PREFIX" ]
+
then
+
	printf "URL for remote '%s' does not have expected prefix '$PREFIX'. Aborting.\n" "$REMOTE" "$PREFIX"
+
	exit 1
+
fi
+

+
ID="${URI:6}"
+
DIR="${RAD_HOME:-$HOME/.radicle}/storage/${ID}/objects"
+

+
if [ ! -d "$DIR" ]
+
then
+
	printf "Directory '%s' is expected to exist, but does not. Aborting.\n" "$DIR"
+
	exit 2
+
fi
+

+
if [ -f "$ALTERNATES" ]
+
then
+
	if ! diff -U3 --color=always --label expected <(echo "$DIR") --label actual "$ALTERNATES"
+
	then
+
		printf "\n'%s' exists and has unexpected contents. Aborting.\n" "${ALTERNATES}"
+
		exit 3
+
	fi
+
else
+
	printf "%s\n" "$DIR" > "$ALTERNATES"
+
fi
+

+
git count-objects -v | grep alternate
added scripts/rad-wire
@@ -0,0 +1,29 @@
+
#! /bin/sh
+
set -eu
+

+
jq -V > /dev/null
+
socat -V > /dev/null
+

+
SOCKET="${RAD_HOME:-"${HOME}/.radicle"}/node/control.sock"
+

+
if [ ! -S "${SOCKET}" ]
+
then
+
	printf "Socket '%s' does not exist. Aborting.\n" "${SOCKET}"
+
	exit 1
+
elif [ -S "${SOCKET}.orig" ]
+
then
+
	printf "Socket '%s' exists. Aborting.\n" "${SOCKET}.orig"
+
	exit 2
+
fi
+

+
mv -v "${SOCKET}" "${SOCKET}.orig"
+
trap "mv -v \"${SOCKET}.orig\" \"${SOCKET}\"" EXIT
+
printf "\n"
+

+
JQ="jq --unbuffered --color-output"
+
JQ_TX="JQ_COLORS='1;30:0;39:0;39:0;39:1;35:1;39:1;39:1;31' $JQ | sed 's/^/>/'"
+
JQ_RX="JQ_COLORS='0;90:0;37:0;37:0;37:0;32:1;37:1;37:1;34' $JQ | sed 's/^/</'"
+

+
socat \
+
	"UNIX-LISTEN:${SOCKET},mode=700,reuseaddr,fork" \
+
	"SYSTEM:\"tee >($JQ_TX) | socat UNIX-CONNECT:${SOCKET}.orig - | tee >($JQ_RX) >&3\",fdout=3"
added scripts/rad-worktree
@@ -0,0 +1,36 @@
+
#! /bin/sh
+
set -eu
+

+
jq -V >/dev/null
+

+
if [ $# -lt 1 ]
+
then
+
	printf 'Usage: %s rad:… [name-of-worktree]\n' "$0"
+
	printf 'The worktree will be created in a new directory within the current working directory.'
+
	exit 1
+
fi
+

+
RID="$1"
+
BARE="${RAD_HOME:-$HOME/.radicle}/storage/${RID:4}"
+
if [ "${RID:0:4}" != "rad:" ]
+
then
+
	printf "Expected RID '%s' to start with 'rad:'. Aborting." "$RID"
+
	exit 2
+
elif [ ! -d "$BARE" ]
+
then
+
	printf "Expected '%s' to exist (maybe invoke \`rad seed\`?). Aborting." "$BARE"
+
	exit 3
+
fi
+
RID="${RID:4}"
+

+
NAME="${2:-$(rad inspect --identity "$RID" | jq -r '.payload."xyz.radicle.project".name')}"
+

+
git -C "$BARE" worktree add "$PWD/$NAME"
+

+
printf "\nYou will very likely want to set up the \`rad\` remote in your worktree,\n"
+
printf "especially if this is the first worktree you create for this repo.\n\n"
+
printf "  1. Change directory to your newly created worktree.\n"
+
printf "         cd %s\n\n" "$NAME"
+
printf "  2. Execute the following two commands.\n"
+
printf "         git remote add rad rad://%s\n" "${RID}"
+
printf "         git remote set-url rad --push rad://%s/%s\n\n" "${RID}" "$(rad self --nid)"