clawdie-ai/scripts/git-doctor.sh
2026-04-25 19:37:54 +02:00

68 lines
1.9 KiB
Bash
Executable file

#!/bin/sh
# git-doctor: catch the shared config corruption that breaks the main
# checkout and sends agents into retry loops.
set -eu
FIX=0
[ "${1:-}" = "--fix" ] && FIX=1
COMMON_DIR=$(git rev-parse --git-common-dir 2>/dev/null || true)
if [ -z "$COMMON_DIR" ]; then
echo "git-doctor: not inside a git repo" >&2
exit 1
fi
SHARED_CONFIG="$COMMON_DIR/config"
if [ ! -f "$SHARED_CONFIG" ]; then
echo "git-doctor: $SHARED_CONFIG missing" >&2
exit 1
fi
STRAY=$(git config --file "$SHARED_CONFIG" --get core.worktree 2>/dev/null || true)
FIXED=0
if [ -n "$STRAY" ]; then
echo "git-doctor: shared $SHARED_CONFIG has core.worktree = $STRAY" >&2
echo " This breaks git commands from the main worktree." >&2
if [ "$FIX" -eq 1 ]; then
git config --file "$SHARED_CONFIG" --unset core.worktree
echo "git-doctor: unset core.worktree from shared config" >&2
FIXED=1
else
echo " Fix: scripts/git-doctor.sh --fix" >&2
exit 1
fi
fi
BARE=$(git config --file "$SHARED_CONFIG" --get core.bare 2>/dev/null || true)
if [ "$BARE" = "true" ] && [ -d "$COMMON_DIR/worktrees" ]; then
echo "git-doctor: shared $SHARED_CONFIG has core.bare = true" >&2
echo " But linked worktrees exist under $COMMON_DIR/worktrees — this is wrong." >&2
if [ "$FIX" -eq 1 ]; then
git config --file "$SHARED_CONFIG" core.bare false
echo "git-doctor: set core.bare = false in shared config" >&2
FIXED=1
else
echo " Fix: scripts/git-doctor.sh --fix" >&2
exit 1
fi
fi
WT_DIR="$COMMON_DIR/worktrees"
if [ -d "$WT_DIR" ]; then
for wt in "$WT_DIR"/*; do
[ -d "$wt" ] || continue
[ -f "$wt/gitdir" ] || continue
target=$(sed -n '1p' "$wt/gitdir")
parent=$(dirname "$target")
if [ ! -d "$parent" ]; then
echo "git-doctor: linked worktree $(basename "$wt") points to missing $parent" >&2
echo " Fix: git worktree prune" >&2
exit 1
fi
done
fi
[ "$FIXED" -eq 1 ] && exit 2
exit 0