Skip to content

Instantly share code, notes, and snippets.

@bosdhill
Last active January 27, 2026 16:23
Show Gist options
  • Select an option

  • Save bosdhill/9df02a444cf3b8351cd2309156cfc36f to your computer and use it in GitHub Desktop.

Select an option

Save bosdhill/9df02a444cf3b8351cd2309156cfc36f to your computer and use it in GitHub Desktop.
Zed gerrit task and keymap for permalink / PR links
#!/bin/sh
#
# Copy this script to ~/.local/bin/zed-gerrit-provider.sh
#
# gerrit - Script for Gerrit permalink & PR link support
case "${1:-}" in
open-review)
# Opens Gerrit review for selected Change-ID
if [ -z "${ZED_SELECTED_TEXT:-}" ]; then
echo "Error: No text selected. Please select a Change-ID."
exit 1
fi
# Use ZED_GERRIT_REVIEW_URL if set
gitiles_domain="$ZED_GERRIT_REVIEW_URL"
if [ -z "$gitiles_domain" ]; then
gitiles_domain="$(git remote get-url origin | cut -f2 -d: | cut -f3- -d/)"
fi
# Construct Gerrit search URL
url="https://$gitiles_domain/q/$ZED_SELECTED_TEXT"
# Open in browser
if [ "$(uname)" = "Darwin" ]; then
open "$url" 2>/dev/null
elif command -v xdg-open >/dev/null; then
xdg-open "$url" 2>/dev/null
else
echo "Open: $url"
fi
echo "Opened: $url"
;;
copy-permalink)
# Copy Gitiles permalink for current file and line
# Expects ZED_RELATIVE_FILE, ZED_ROW, and git context
# Get git remote URL and convert to Gitiles format
remote_url=$(git remote get-url origin 2>/dev/null || echo "")
if [ -z "$remote_url" ]; then
echo "Error: Not in a git repository or no origin remote"
exit 1
fi
# Extract project path from remote URL
# Handles formats like:
# https://gerrit.example.com/project
# ssh://[email protected]:29418/project
# [email protected]:project
gitiles_base=$(echo "$remote_url" | sed -E 's|.*://[^/]+/||; s|.*:([0-9]+/)?||; s|\.git$||')
# Use ZED_GERRIT_REVIEW_URL if set
gitiles_domain="$ZED_GERRIT_REVIEW_URL"
if [ -z "$gitiles_domain" ]; then
gitiles_domain="$(git remote get-url origin | cut -f2 -d: | cut -f3- -d/)"
fi
# Construct Gitiles URL
# Format: <gitiles_domain>/+/<commit>/<file>#<line>
file="${ZED_RELATIVE_FILE:-}"
line="${ZED_ROW:-1}"
# Get the most recent commit that touched this file at its current path
# This ensures the file exists at this path in the commit
# Similar to GitLens gitlens.remotes "fileInCommit" URL pattern where ${id}
# is the latest commit
commit=$(git log -1 --format=%H -- "$file" 2>/dev/null) || {
echo "Error: Could not find any commits for $file" >&2
exit 1
}
if [ -z "$commit" ]; then
echo "Error: File has no commit history (untracked?)" >&2
exit 1
fi
url="https://$gitiles_domain/plugins/gitiles/$gitiles_base/+/$commit/$file#$line"
# Copy to clipboard using OSC 52 (works in most terminals)
printf "\033]52;c;%s\a" "$(printf "%s" "$url" | base64)"
# Also try native clipboard
if [ "$(uname)" = "Darwin" ]; then
printf "%s" "$url" | pbcopy 2>/dev/null || true
elif command -v xclip >/dev/null; then
printf "%s" "$url" | xclip -selection clipboard 2>/dev/null || true
elif command -v xsel >/dev/null; then
printf "%s" "$url" | xsel --clipboard 2>/dev/null || true
elif command -v wl-copy >/dev/null; then
printf "%s" "$url" | wl-copy 2>/dev/null || true
fi
echo "Copied: $url"
;;
*)
echo "Unknown action: ${1:-}" >&2
echo "Usage: $0 {open-review|copy-permalink}" >&2
exit 1
;;
esac
// Add the BEGIN/END block to your ~/.config/zed/keymap.json
[
// BEGIN zed-gerrit-provider
{
"context": "Workspace",
"bindings": {
"ctrl-cmd-g": [
"task::Spawn",
{
"reevaluate_context": true,
"task_name": "gerrit_open_review",
},
],
"ctrl-cmd-k": [
"task::Spawn",
{
"reevaluate_context": true,
"task_name": "gerrit_copy_permalink",
},
],
},
},
// END zed-gerrit-provider
]
// Add the BEGIN/END block to your ~/.config/zed/tasks.json
[
// BEGIN zed-gerrit-provider
{
"label": "gerrit_open_review",
"command": "~/.local/bin/zed-gerrit-provider.sh",
"args": ["open-review"],
"env": {
// Set this to your gerrit review UI domain if its
// different than `git remote -v | grep origin`.
//
// For e.g., Google uses https://gerrit-review.googlesource.com
//
"ZED_GERRIT_REVIEW_URL": "",
},
"allow_concurrent_runs": false,
// Uncomment these to disable terminal pop-ups
// "reveal": "never",
// "show_command": false,
// "hide": "always",
},
{
"label": "gerrit_copy_permalink",
"command": "~/.local/bin/zed-gerrit-provider.sh",
"args": ["copy-permalink"],
"env": {
// Set this to your gerrit review UI domain if its
// different than `git remote -v | grep origin`.
//
// For e.g., Google uses https://gerrit-review.googlesource.com
//
"ZED_GERRIT_REVIEW_URL": "",
},
"allow_concurrent_runs": false,
// Uncomment these to disable terminal pop-ups
// "reveal": "never",
// "show_command": false,
// "hide": "always",
},
// END zed-gerrit-provider
]
@bosdhill
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment