Skip to content

Instantly share code, notes, and snippets.

@canadaduane
Created February 25, 2026 20:45
Show Gist options
  • Select an option

  • Save canadaduane/255b9ac92dfaaf7a0970b4a878356d89 to your computer and use it in GitHub Desktop.

Select an option

Save canadaduane/255b9ac92dfaaf7a0970b4a878356d89 to your computer and use it in GitHub Desktop.
Preview a JJ revision; I use this in jjui to show me a synopsis before all the details of a revision
#!/bin/bash
# JJUI Preview Script: Shows revision info with truncated description,
# file summary, diff, and optionally the full description at the end.
#
# Usage: preview-revision.sh <change_id>
#
# Output order:
# 1. Author + Date
# 2. Description (truncated to MAX_DESC_LINES)
# 3. Changed Files summary
# 4. Full Diff
# 5. Full Description (only if truncated above)
set -euo pipefail
MAX_DESC_LINES=5
CHANGE_ID="${1:?Usage: preview-revision.sh <change_id>}"
# --- Separator that won't appear in commit messages ---
SEP="---JJUI_PREVIEW_SEP_7f3a---"
# Command 1: All metadata in one call.
# We use a unique text separator between fields.
METADATA=$(jj log -r "$CHANGE_ID" --no-graph --no-pager --ignore-working-copy -T "
author ++ \"\\n${SEP}\\n\" ++
committer.timestamp().format(\"%Y-%m-%d %H:%M\") ++ \"\\n${SEP}\\n\" ++
description ++ \"\\n${SEP}\\n\" ++
diff.summary()
")
# Parse fields by splitting on the separator.
# We use awk to split into sections.
AUTHOR=$(echo "$METADATA" | awk "/$SEP/{n++; next} n==0{print}")
DATE=$(echo "$METADATA" | awk "/$SEP/{n++; next} n==1{print}")
DESC=$(echo "$METADATA" | awk "/$SEP/{n++; next} n==2{print}")
FILE_SUMMARY=$(echo "$METADATA" | awk "/$SEP/{n++; next} n==3{print}")
# Remove leading/trailing blank lines from description
DESC=$(echo "$DESC" | sed '/./,$!d' | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}')
if [ -z "$DESC" ]; then
DESC="(no description)"
fi
# Output header
echo "Author: $AUTHOR"
echo "Date: $DATE"
echo ""
# Truncate description if needed
LINE_COUNT=$(echo "$DESC" | wc -l | tr -d ' ')
TRUNCATED=false
if [ "$LINE_COUNT" -gt "$MAX_DESC_LINES" ]; then
echo "$DESC" | head -"$MAX_DESC_LINES"
REMAINING=$((LINE_COUNT - MAX_DESC_LINES))
echo " ...($REMAINING more lines, see bottom)"
TRUNCATED=true
else
echo "$DESC"
fi
echo ""
echo "━━━ Changed Files ━━━"
# Colorize file status using 256-color (subtle/muted tones)
echo "$FILE_SUMMARY" | while IFS= read -r line; do
case "$line" in
M\ *) printf '\033[38;5;179m%s\033[0m\n' "$line" ;; # muted gold
A\ *) printf '\033[38;5;108m%s\033[0m\n' "$line" ;; # muted sage green
D\ *) printf '\033[38;5;167m%s\033[0m\n' "$line" ;; # muted rose
R\ *) printf '\033[38;5;110m%s\033[0m\n' "$line" ;; # muted steel blue
C\ *) printf '\033[38;5;139m%s\033[0m\n' "$line" ;; # muted lavender
*) echo "$line" ;;
esac
done
echo ""
echo "━━━ Diff ━━━"
# Command 2: Only the diff (templates can't produce colored diff output)
jj diff -r "$CHANGE_ID" --no-pager --color always --ignore-working-copy
# Show full description at end only if truncated
if [ "$TRUNCATED" = true ]; then
echo ""
echo "━━━ Full Description ━━━"
echo "$DESC"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment