Files
fn_registry/bash/functions/infra/git_pull_with_stash.sh
T
egutierrez 625569485f feat(doctor): add fn doctor CLI + 14 functions for system management
Adds `fn doctor` read-only diagnostic command with subcommands artefacts,
services, sync, uses-functions, unused, and --json flag for agents.
Each subcommand wraps a registry function in functions/infra/.

New functions:
- artefact_doctor, services_status, pc_locations_drift,
  audit_uses_functions, find_unused_functions (Go diagnostics)
- backup_sqlite_db, rotate_backups, wait_for_http, wait_for_port,
  port_kill, tail_journal, pre_commit_hook_install (bash utilities)
- notify_telegram (Go HTTP)
- backup_all pipeline (tag launcher)

Plus prior session leftovers (scan_secrets_in_dirty, append_diary_entry,
git utilities, http_session_cookie_middleware, compile/full-git pipelines).

Fixes pc_locations_drift filepath.Join bug with absolute dir_path.
Documents fn doctor in CLAUDE.md, .claude/rules/fn_doctor.md (rule 23),
docs/architecture.md, CHANGELOG.md (2026-05-07), and diary entry.

First fn doctor uses-functions run found drift in 7/12 apps (deuda
para sincronizar app.md con imports reales).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-07 01:42:10 +02:00

81 lines
2.3 KiB
Bash

#!/usr/bin/env bash
# git_pull_with_stash — Si el repo tiene cambios, los stashea antes de pullear.
# Hace fetch + pull --ff-only. Si hay divergencia, restaura el stash y reporta.
# Exit 0 siempre (el caller decide como manejar los errores).
git_pull_with_stash() {
local repo_dir="${1:-.}"
if [[ ! -d "$repo_dir/.git" ]]; then
echo "git_pull_with_stash: '$repo_dir' no es un repo git" >&2
return 1
fi
local abs_repo
abs_repo="$(cd "$repo_dir" && pwd)"
local repo_name
repo_name="$(basename "$abs_repo")"
# Comprobar si hay cambios pendientes
local dirty
dirty=$(git -C "$abs_repo" status --porcelain | wc -l)
local stashed=0
if [[ "$dirty" -gt 0 ]]; then
git -C "$abs_repo" stash push \
--include-untracked \
-m "auto-stash before pull" \
>/dev/null 2>&1 || true
stashed=1
fi
# Fetch origin
git -C "$abs_repo" fetch origin >/dev/null 2>&1 || {
# Sin remote configurado o sin red — considerar up-to-date
if [[ "$stashed" -eq 1 ]]; then
git -C "$abs_repo" stash pop >/dev/null 2>&1 || true
fi
echo "[up-to-date] $repo_name (no remote)"
return 0
}
# Pull --ff-only
local pull_out
pull_out=$(git -C "$abs_repo" pull --ff-only 2>&1)
local pull_exit=$?
if [[ $pull_exit -ne 0 ]]; then
# Divergencia — restaurar stash y reportar
if [[ "$stashed" -eq 1 ]]; then
git -C "$abs_repo" stash pop >/dev/null 2>&1 || true
fi
echo "[diverged] $repo_name"
return 0
fi
# Pull exitoso — restaurar stash si habia
if [[ "$stashed" -eq 1 ]]; then
local pop_out
pop_out=$(git -C "$abs_repo" stash pop 2>&1)
local pop_exit=$?
if [[ $pop_exit -ne 0 ]]; then
echo "[stash-conflict] $repo_name"
return 0
fi
fi
# Determinar si se trajo algo nuevo
if echo "$pull_out" | grep -q "Already up to date"; then
echo "[up-to-date] $repo_name"
else
local commits
commits=$(echo "$pull_out" | grep -c 'Fast-forward\|commit\|Merge' || true)
echo "[pulled] $repo_name"
fi
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
git_pull_with_stash "$@"
fi