|
| 1 | +#!/usr/bin/env bash |
| 2 | +# lpb-config — Manage the LocalPibox Pi config repo inside the container |
| 3 | +# |
| 4 | +# Usage: |
| 5 | +# lpb-config status — Show current commit, remote HEAD, local changes |
| 6 | +# lpb-config update — Fetch + fast-forward (safe: refuses on local changes) |
| 7 | +# lpb-config reset — Re-clone, destroy local changes (with confirmation) |
| 8 | +# lpb-config merge — Open git merge flow for advanced users |
| 9 | + |
| 10 | +set -euo pipefail |
| 11 | + |
| 12 | +AGENT_DIR="${1:-/home/lpb/.pi}" |
| 13 | +CONFIG_REMOTE="${2:-https://github.com/localpibox/config.git}" |
| 14 | +CONFIG_REF="${3:-main}" |
| 15 | + |
| 16 | +# ─── Helpers ──────────────────────────────────────────────────────────────── |
| 17 | +info() { echo "[lpb-config] $*"; } |
| 18 | +warn() { echo "[lpb-config] $*" >&2; } |
| 19 | +err() { echo "[lpb-config] ERROR: $*" >&2; exit 1; } |
| 20 | + |
| 21 | +# ─── status ───────────────────────────────────────────────────────────────── |
| 22 | +cmd_status() { |
| 23 | + if [[ ! -d "${AGENT_DIR}/.git" ]]; then |
| 24 | + warn "No config repo at ${AGENT_DIR}" |
| 25 | + echo " Run 'lpb-config update' to clone it." |
| 26 | + exit 0 |
| 27 | + fi |
| 28 | + |
| 29 | + local cur_head remote_head local_changes |
| 30 | + cur_head=$(git -C "${AGENT_DIR}" rev-parse HEAD 2>/dev/null || echo "?") |
| 31 | + cur_short="${cur_head:0:8}" |
| 32 | + |
| 33 | + info "Current: ${cur_short} ($(git -C "${AGENT_DIR}" log --oneline -1 2>/dev/null || echo '?'))" |
| 34 | + |
| 35 | + if ! git -C "${AGENT_DIR}" remote get-url origin &>/dev/null; then |
| 36 | + warn "No remote configured" |
| 37 | + exit 0 |
| 38 | + fi |
| 39 | + |
| 40 | + remote_head=$(git -C "${AGENT_DIR}" rev-parse "origin/${CONFIG_REF}" 2>/dev/null || echo "") |
| 41 | + if [[ -n "$remote_head" ]]; then |
| 42 | + remote_short="${remote_head:0:8}" |
| 43 | + info "Remote: ${remote_short} origin/${CONFIG_REF}" |
| 44 | + fi |
| 45 | + |
| 46 | + # Check for uncommitted local changes |
| 47 | + local_changes=$(git -C "${AGENT_DIR}" status --porcelain 2>/dev/null || echo "") |
| 48 | + if [[ -n "$local_changes" ]]; then |
| 49 | + warn "Uncommitted changes detected (${#local_changes} lines):" |
| 50 | + echo "$local_changes" | head -10 |
| 51 | + else |
| 52 | + info "Working tree: clean" |
| 53 | + fi |
| 54 | + |
| 55 | + # Check if behind remote |
| 56 | + if [[ -n "$remote_head" ]]; then |
| 57 | + if git -C "${AGENT_DIR}" merge-base --is-ancestor "$cur_head" "$remote_head" 2>/dev/null; then |
| 58 | + info "Status: up to date (or ahead)" |
| 59 | + else |
| 60 | + warn "Status: behind remote — run 'lpb-config update' to fetch" |
| 61 | + fi |
| 62 | + fi |
| 63 | +} |
| 64 | + |
| 65 | +# ─── update ───────────────────────────────────────────────────────────────── |
| 66 | +cmd_update() { |
| 67 | + if [[ ! -d "${AGENT_DIR}/.git" ]]; then |
| 68 | + info "Cloning config repo from ${CONFIG_REMOTE}..." |
| 69 | + git clone --depth=1 --branch "${CONFIG_REF}" "${CONFIG_REMOTE}" "${AGENT_DIR}" |
| 70 | + info "Cloned. Run 'lpb-config status' to verify." |
| 71 | + exit 0 |
| 72 | + fi |
| 73 | + |
| 74 | + info "Fetching updates from ${CONFIG_REMOTE}..." |
| 75 | + if ! git -C "${AGENT_DIR}" fetch origin "${CONFIG_REF}" 2>/dev/null; then |
| 76 | + err "Failed to fetch from ${CONFIG_REMOTE}" |
| 77 | + fi |
| 78 | + |
| 79 | + remote_head=$(git -C "${AGENT_DIR}" rev-parse "origin/${CONFIG_REF}" 2>/dev/null) |
| 80 | + cur_head=$(git -C "${AGENT_DIR}" rev-parse HEAD 2>/dev/null) |
| 81 | + |
| 82 | + if [[ "$remote_head" = "$cur_head" ]]; then |
| 83 | + info "Already up to date." |
| 84 | + exit 0 |
| 85 | + fi |
| 86 | + |
| 87 | + # Check for local changes — refuse to overwrite |
| 88 | + local_changes=$(git -C "${AGENT_DIR}" status --porcelain 2>/dev/null || echo "") |
| 89 | + if [[ -n "$local_changes" ]]; then |
| 90 | + warn "Uncommitted changes detected:" |
| 91 | + echo "$local_changes" |
| 92 | + warn "Aborting — local changes would be lost." |
| 93 | + echo "" |
| 94 | + echo "Options:" |
| 95 | + echo " 1. Commit your changes first, then re-run 'lpb-config update'" |
| 96 | + echo " 2. Use 'lpb-config reset' to discard local changes (destructive)" |
| 97 | + echo " 3. Use 'lpb-config merge' for an interactive git merge" |
| 98 | + exit 1 |
| 99 | + fi |
| 100 | + |
| 101 | + # Fast-forward |
| 102 | + if git -C "${AGENT_DIR}" merge-base --is-ancestor "$cur_head" "$remote_head" 2>/dev/null; then |
| 103 | + git -C "${AGENT_DIR}" reset --hard "origin/${CONFIG_REF}" |
| 104 | + info "Updated to $(git -C "${AGENT_DIR}" log --oneline -1)" |
| 105 | + else |
| 106 | + warn "Cannot fast-forward — conflicts detected." |
| 107 | + echo "" |
| 108 | + echo "Run 'lpb-config merge' for an interactive merge, or" |
| 109 | + echo "use 'lpb-config reset' to discard local changes (destructive)." |
| 110 | + exit 1 |
| 111 | + fi |
| 112 | +} |
| 113 | + |
| 114 | +# ─── reset ────────────────────────────────────────────────────────────────── |
| 115 | +cmd_reset() { |
| 116 | + if [[ ! -d "${AGENT_DIR}/.git" ]]; then |
| 117 | + info "Cloning config repo from ${CONFIG_REMOTE}..." |
| 118 | + git clone --depth=1 --branch "${CONFIG_REF}" "${CONFIG_REMOTE}" "${AGENT_DIR}" |
| 119 | + info "Cloned." |
| 120 | + exit 0 |
| 121 | + fi |
| 122 | + |
| 123 | + warn "This will destroy ALL local changes in ${AGENT_DIR}." |
| 124 | + echo " Current: $(git -C "${AGENT_DIR}" rev-parse HEAD 2>/dev/null | head -c 8)" |
| 125 | + echo " Remote: $(git -C "${AGENT_DIR}" rev-parse origin/${CONFIG_REF} 2>/dev/null | head -c 8)" |
| 126 | + |
| 127 | + if [[ "${FORCE:-}" != "1" ]]; then |
| 128 | + echo -n " Continue? [y/N]: " |
| 129 | + read -r answer |
| 130 | + if [[ "$answer" != "y" && "$answer" != "Y" ]]; then |
| 131 | + info "Aborted." |
| 132 | + exit 0 |
| 133 | + fi |
| 134 | + fi |
| 135 | + |
| 136 | + rm -rf "${AGENT_DIR}" |
| 137 | + git clone --depth=1 --branch "${CONFIG_REF}" "${CONFIG_REMOTE}" "${AGENT_DIR}" |
| 138 | + info "Reset complete. Re-run Pi to apply the new config." |
| 139 | +} |
| 140 | + |
| 141 | +# ─── merge ────────────────────────────────────────────────────────────────── |
| 142 | +cmd_merge() { |
| 143 | + if [[ ! -d "${AGENT_DIR}/.git" ]]; then |
| 144 | + err "No config repo at ${AGENT_DIR}. Run 'lpb-config update' first." |
| 145 | + fi |
| 146 | + |
| 147 | + info "Fetching latest from ${CONFIG_REMOTE}..." |
| 148 | + git -C "${AGENT_DIR}" fetch origin "${CONFIG_REF}" |
| 149 | + |
| 150 | + remote_head=$(git -C "${AGENT_DIR}" rev-parse "origin/${CONFIG_REF}" 2>/dev/null) |
| 151 | + cur_head=$(git -C "${AGENT_DIR}" rev-parse HEAD 2>/dev/null) |
| 152 | + |
| 153 | + if [[ "$remote_head" = "$cur_head" ]]; then |
| 154 | + info "Already up to date." |
| 155 | + exit 0 |
| 156 | + fi |
| 157 | + |
| 158 | + info "Starting merge: ${cur_head:0:8} → ${remote_head:0:8}" |
| 159 | + if git -C "${AGENT_DIR}" merge --no-edit "origin/${CONFIG_REF}"; then |
| 160 | + info "Merge complete." |
| 161 | + else |
| 162 | + warn "Merge conflicts detected. Resolve them with git, then:" |
| 163 | + echo "" |
| 164 | + echo " cd ${AGENT_DIR}" |
| 165 | + echo " git add <resolved-files>" |
| 166 | + echo " git commit" |
| 167 | + echo " lpb-config status" |
| 168 | + exit 1 |
| 169 | + fi |
| 170 | +} |
| 171 | + |
| 172 | +# ─── Dispatcher ───────────────────────────────────────────────────────────── |
| 173 | +case "${1:-help}" in |
| 174 | + status) cmd_status ;; |
| 175 | + update) cmd_update ;; |
| 176 | + reset) cmd_reset ;; |
| 177 | + merge) cmd_merge ;; |
| 178 | + help|--help|-h) |
| 179 | + echo "Usage: lpb-config [command]" |
| 180 | + echo "" |
| 181 | + echo "Commands:" |
| 182 | + echo " status — Show current commit, remote HEAD, local changes" |
| 183 | + echo " update — Fetch + fast-forward (safe: refuses on local changes)" |
| 184 | + echo " reset — Re-clone, destroy local changes (with confirmation)" |
| 185 | + echo " merge — Open git merge flow for advanced users" |
| 186 | + echo "" |
| 187 | + echo "Environment:" |
| 188 | + echo " AGENT_DIR — Config repo path (default: /home/lpb/.pi)" |
| 189 | + echo " CONFIG_REMOTE — Git remote URL (default: https://github.com/localpibox/config.git)" |
| 190 | + echo " CONFIG_REF — Branch to track (default: main)" |
| 191 | + ;; |
| 192 | + *) |
| 193 | + err "Unknown command: $1. Use 'lpb-config help' for usage." |
| 194 | + ;; |
| 195 | +esac |
0 commit comments