find-project-root
#!/bin/bash
# Single source of truth for "what is the current project root".
#
# Usage: find-project-root [PATH]
# PATH defaults to cwd.
#
# On success: prints the absolute path of the project root and exits 0.
# On failure: prints nothing and exits 1.
#
# Definition of "project root":
# - The top of the git work tree containing PATH.
# - If PATH is not inside any git work tree, there is no project root
# (and we report that by exiting 1 with no output).
#
# This is deliberately strict. Other compass scripts that want a softer
# fallback (e.g. "use cwd if there's no project") apply that fallback
# themselves -- it's a property of those scripts, not of the underlying
# notion of "project".
#
# FUTURE: when we add other project-marker conventions
# (e.g. a top-level `.project` file, or a configured marker name),
# this is the one place that resolves them.
set -euo pipefail
start="${1:-$PWD}"
if [ ! -d "$start" ]; then
echo "find-project-root: not a directory: $start" >&2
exit 2
fi
if root=$(cd "$start" && git rev-parse --show-toplevel 2>/dev/null); then
echo "$root"
exit 0
fi
exit 1
find-project-root · 1.1 KiB · 9920325da1