Files
fn_registry/bash/functions/infra/git_push_if_ahead.sh
T
egutierrez 2a3d780347 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

67 lines
2.1 KiB
Bash

#!/usr/bin/env bash
# git_push_if_ahead — Decide si pushear un repo git sin tocar la red para decidir.
# Usa refs locales (rev-list) para determinar si hay commits por enviar.
# Stdout: linea de estado con [push|push -u|skip|error].
git_push_if_ahead() {
local repo_dir="${1:-.}"
if [[ ! -d "$repo_dir/.git" ]]; then
echo "git_push_if_ahead: '$repo_dir' no es un repo git" >&2
return 1
fi
local abs_repo
abs_repo="$(cd "$repo_dir" && pwd)"
# Nombre corto para display (basename del path)
local repo_name
repo_name="$(basename "$abs_repo")"
# Rama actual
local branch
branch=$(git -C "$abs_repo" rev-parse --abbrev-ref HEAD 2>/dev/null)
if [[ -z "$branch" || "$branch" == "HEAD" ]]; then
echo "[error] $repo_name: no se puede determinar la rama actual"
return 0
fi
# Comprobar si existe upstream configurado
local upstream
upstream=$(git -C "$abs_repo" rev-parse --abbrev-ref --symbolic-full-name '@{u}' 2>/dev/null || true)
if [[ -z "$upstream" ]]; then
# Sin upstream: push -u para establecer tracking
echo "[push -u] $repo_name ($branch)" >&2
local push_out
push_out=$(git -C "$abs_repo" push -u origin "$branch" 2>&1) || {
echo "[error] $repo_name: $push_out"
return 0
}
echo "$push_out" | tail -3 >&2
echo "[push -u] $repo_name ($branch)"
return 0
fi
# Contar commits locales que no estan en upstream (sin red)
local ahead
ahead=$(git -C "$abs_repo" rev-list --count '@{u}..HEAD' 2>/dev/null || echo "0")
if [[ "${ahead:-0}" -gt 0 ]]; then
echo "[push] $repo_name ($branch, $ahead commits ahead)" >&2
local push_out
push_out=$(git -C "$abs_repo" push origin "$branch" 2>&1) || {
echo "[error] $repo_name: $(echo "$push_out" | tail -1)"
return 0
}
echo "$push_out" | tail -3 >&2
echo "[push] $repo_name ($branch, $ahead commits ahead)"
else
echo "[skip] $repo_name (up-to-date)"
fi
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
git_push_if_ahead "$@"
fi