orient-git
#!/bin/bash
# Briefly describes the git repo surrounding the current working directory.
#
# Silent (no output, exit 0) when cwd is not inside a git repo.
# Emits a markdown fragment intended to sit under an h3 or smaller.
#
# Reports: repo root, current branch, last commit (short), and worktree dirtiness.
set -euo pipefail
# Bail silently if not inside a repo.
# `rev-parse --is-inside-work-tree` prints to stderr on miss; squelch it.
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
exit 0
fi
root=$(git rev-parse --show-toplevel)
# Branch: prefer symbolic name; fall back to short sha on detached HEAD.
branch=$(git symbolic-ref --quiet --short HEAD || git rev-parse --short HEAD)
# Last commit: short sha, relative date, subject.
# `--no-show-signature` keeps output stable across gpg config.
last_commit=$(git log -1 --no-show-signature --format='%h %cr -- %s' 2>/dev/null || echo '(no commits yet)')
# Worktree status: counts of tracked-modified vs untracked entries.
# `--porcelain=v1` is stable; untracked lines start with `??`,
# anything else with content is a tracked change.
# `grep -c` exits 1 on no match, which would trip `set -e`; `|| true` neutralizes that.
status=$(git status --porcelain=v1 2>/dev/null || true)
if [ -z "$status" ]; then
n_dirty=0
n_untracked=0
else
n_untracked=$(printf '%s\n' "$status" | grep -c '^??' || true)
n_dirty=$(printf '%s\n' "$status" | grep -cv '^??' || true)
fi
echo "- repo root: \`${root}\`"
echo "- branch: \`${branch}\`"
echo "- last commit: ${last_commit}"
echo "- worktree: ${n_dirty} modified tracked, ${n_untracked} untracked."
orient-git · 1.6 KiB · 9920325da1