fix: auto-configure git safe.directory in setup.sh for /opt-style installs

Git 2.35.2+ rejects repos where directory owner != current user, which
is the common case when cloned as root into /opt. setup.sh now detects
this and calls git config --global --add safe.directory automatically.
When run via sudo, it writes into SUDO_USER's config rather than root's.
README updated with both fixes: git safe.directory and chown for preflight.
This commit is contained in:
pyr0ball 2026-02-26 22:07:39 -08:00
parent c287392c39
commit 6dd89a0863
2 changed files with 36 additions and 2 deletions

View file

@ -42,13 +42,21 @@ make start PROFILE=single-gpu
### Installing to `/opt` or other system directories
If you clone into a root-owned directory, fix ownership first so preflight can write `.env` and `compose.override.yml`:
If you clone into a root-owned directory (e.g. `sudo git clone ... /opt/peregrine`), two things need fixing:
**1. Git ownership warning** (`fatal: detected dubious ownership`) — `./manage.sh setup` fixes this automatically. If you need git to work *before* running setup:
```bash
git config --global --add safe.directory /opt/peregrine
```
**2. Preflight write access** — preflight writes `.env` and `compose.override.yml` into the repo directory. Fix ownership once:
```bash
sudo chown -R $USER:$USER /opt/peregrine
```
Then run without `sudo` — Peregrine doesn't need it.
After that, run everything without `sudo`.
### Podman

View file

@ -64,6 +64,31 @@ install_build_tools() {
success "make installed."
}
# ── Git safe.directory ─────────────────────────────────────────────────────────
# Git 2.35.2+ rejects repos where the directory owner != current user.
# Common when cloned as root into /opt and then run as a regular user.
# Fix by registering the repo path in the appropriate user's git config.
configure_git_safe_dir() {
local repo_dir
repo_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# If git is happy already, nothing to do
if git -C "$repo_dir" rev-parse --git-dir &>/dev/null 2>&1; then
success "Git repository ownership OK."
return
fi
info "Configuring git safe.directory for $repo_dir"
if [[ -n "${SUDO_USER:-}" ]]; then
# Running under sudo — write into the invoking user's config, not root's
sudo -u "$SUDO_USER" git config --global --add safe.directory "$repo_dir"
success "safe.directory set for user '${SUDO_USER}'."
else
git config --global --add safe.directory "$repo_dir"
success "safe.directory set."
fi
}
# ── Git ────────────────────────────────────────────────────────────────────────
install_git() {
if cmd_exists git; then success "git already installed: $(git --version)"; return; fi
@ -317,6 +342,7 @@ main() {
install_build_tools
install_git
configure_git_safe_dir
# Podman takes precedence if already installed; otherwise install Docker
if ! check_podman; then
install_docker