Skip to content

Instantly share code, notes, and snippets.

@scottwater
Last active August 7, 2026 14:54
Show Gist options
  • Select an option

  • Save scottwater/9467447340c62875d955b50d16e9e19c to your computer and use it in GitHub Desktop.

Select an option

Save scottwater/9467447340c62875d955b50d16e9e19c to your computer and use it in GitHub Desktop.
Stooges Herdr setup and teardown hooks. Add the two sh files with execute permission and then add the setup and teardown section to your .stooges-metadata.json file - Then just stooges branch|track|pr as normal and it will create the workspace in both stooges and herdr (if you run it from within herdr)
{
"setupScript": "stooges-setup.sh",
"teardownScript": "stooges-teardown.sh"
}
#!/usr/bin/env bash
set -euo pipefail
# Stooges setup hook: open the new workspace in Herdr and focus it.
if [[ -z "${STOOGES_FOLDER:-}" ]]; then
echo "error: STOOGES_FOLDER is not set; run this as a Stooges setup hook" >&2
exit 1
fi
for required_command in git herdr; do
command -v "$required_command" >/dev/null 2>&1 || {
echo "error: required command '$required_command' was not found on PATH" >&2
exit 1
}
done
workspace_path="${STOOGES_FOLDER_PATH:-$PWD}"
if [[ ! -d "$workspace_path" ]]; then
echo "error: workspace directory does not exist: $workspace_path" >&2
exit 1
fi
workspace_path="$(cd "$workspace_path" && pwd -P)"
if ! git_dir="$(git -C "$workspace_path" rev-parse --absolute-git-dir 2>/dev/null)"; then
echo "error: workspace is not a Git repository: $workspace_path" >&2
exit 1
fi
create_response="$(herdr workspace create \
--cwd "$workspace_path" \
--label "$STOOGES_PROJECT $STOOGES_FOLDER" \
--focus)"
workspace_id="$(printf '%s\n' "$create_response" |
sed -n 's/.*"workspace_id"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p')"
if [[ ! "$workspace_id" =~ ^[[:alnum:]_:-]+$ ]]; then
echo "error: Herdr did not return a valid workspace ID" >&2
exit 1
fi
# Keep lifecycle state in Git's private directory so it does not dirty the workspace.
printf '%s\n' "$workspace_id" >"$git_dir/stooges-herdr-workspace-id"
echo "Opened and focused Herdr workspace: $STOOGES_FOLDER ($workspace_id)"
#!/usr/bin/env bash
set -euo pipefail
# Stooges teardown hook: close the Herdr workspace created by stooges-setup.sh.
if [[ -z "${STOOGES_FOLDER:-}" ]]; then
echo "error: STOOGES_FOLDER is not set; run this as a Stooges teardown hook" >&2
exit 1
fi
for required_command in git herdr; do
command -v "$required_command" >/dev/null 2>&1 || {
echo "error: required command '$required_command' was not found on PATH" >&2
exit 1
}
done
workspace_path="${STOOGES_FOLDER_PATH:-$PWD}"
if [[ ! -d "$workspace_path" ]]; then
echo "error: workspace directory does not exist: $workspace_path" >&2
exit 1
fi
workspace_path="$(cd "$workspace_path" && pwd -P)"
if ! git_dir="$(git -C "$workspace_path" rev-parse --absolute-git-dir 2>/dev/null)"; then
echo "error: workspace is not a Git repository: $workspace_path" >&2
exit 1
fi
state_file="$git_dir/stooges-herdr-workspace-id"
if [[ ! -f "$state_file" ]]; then
echo "No recorded Herdr workspace for $STOOGES_FOLDER; nothing to close."
exit 0
fi
workspace_id="$(<"$state_file")"
if [[ ! "$workspace_id" =~ ^[[:alnum:]_:-]+$ ]]; then
echo "error: invalid Herdr workspace ID in $state_file" >&2
exit 1
fi
workspace_list="$(herdr workspace list)"
if ! grep -Eq "\"workspace_id\"[[:space:]]*:[[:space:]]*\"${workspace_id}\"" <<< "$workspace_list"; then
rm -f "$state_file"
echo "Herdr workspace $workspace_id is already closed."
exit 0
fi
herdr workspace close "$workspace_id"
rm -f "$state_file"
echo "Closed Herdr workspace: $STOOGES_FOLDER ($workspace_id)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment