#!/usr/bin/env bash # © 2026 Delminio — tutti i diritti riservati. Software proprietario: l'uso con Delminio è regolato dal contratto con cui si usa il servizio; la lettura e la verifica da LICENCE. case "${1:-}" in nuovo|merge|smonta) principale=$(cd "$(dirname "$(_assoluto "$0")")/.." && pwd) if f=$(chiedi GET /api/funzioni); then perche=$(printf '%s' "$f" | python3 -c 'import json,sys x = next((x for x in json.load(sys.stdin)["funzioni"] if x["id"] == "agenti"), {}) print("" if x.get("accesa", True) else "licenza" if x.get("concessa") is False else "spenta")' 2>/dev/null || true) case "$perche" in licenza) echo "$M_piu_agenti_non_concessa" >&2; exit 1 ;; spenta) echo "$M_piu_agenti_insieme" >&2; exit 1 ;; esac fi remoto=$(git -C "$principale" remote get-url origin 2>/dev/null || true) pid=$(cli_pid || true); nome="${2:-}"; [ -z "$nome" ] && [ -n "$pid" ] && nome=$(mia_sessione "$pid") [ -n "$nome" ] || { _f "$M_chi_sei_bash" gesto "$1" >&2; exit 1; } qui=$(git rev-parse --show-toplevel 2>/dev/null || echo "$principale") if [ -z "${DELMINIO_WORKTREE_DEL_PROGETTO:-}" ] && [ -x "$qui/.delminio/worktree" ]; then DELMINIO_WORKTREE_DEL_PROGETTO=1 exec "$qui/.delminio/worktree" "$1" "$nome" fi ramo="sessione/$nome"; wt="$(dirname "$principale")/$(basename "$principale")-$nome" case "$1" in nuovo) [ -e "$wt" ] && { _f "$M_gia_entraci_smontalo" wt "$wt" >&2; exit 1; } [ -z "$remoto" ] || git -C "$principale" pull -q --ff-only 2>/dev/null || echo "$M_principale_aggiorna_remoto" >&2 git -C "$principale" worktree add -q "$wt" -b "$ramo" || exit 1 for f in $KIT_FILE; do [ -e "$principale/$f" ] && mkdir -p "$wt/$(dirname "$f")" && ln -sfn "$principale/$f" "$wt/$f" done _f "$M_ramo" wt "$wt" ramo "$ramo" case "$( [ -n "$pid" ] && cli_di "$pid")" in codex) _f "$M_codex_resta_dov" wt "$wt" ;; claude) echo "$M_entraci_col_tool" echo "$M_lavoro_tocca_altro" ;; *) _f "$M_tua_cli_modo" wt "$wt" ;; esac ;; merge) dove=$(git rev-parse --show-toplevel) [ "$dove" = "$wt" ] || { _f "$M_merge_lancia_tuo" wt "$wt" dove "$dove" >&2; exit 1; } [ -z "$(git status --porcelain --untracked-files=no)" ] || { echo "$M_hai_lavoro_committato" >&2; git status --short | head -10 >&2; exit 1; } exec 9>"$(git rev-parse --git-common-dir)/delminio-merge.lock" _lucchetto 600 || { echo "$M_altro_merge_corso" >&2; exit 1; } staged=$(git -C "$principale" diff --cached --name-only) [ -z "$staged" ] || { echo "$M_principale_lavoro_staging" >&2; echo "$staged" | head -10 >&2; exit 1; } [ -z "$remoto" ] || git -C "$principale" pull -q --ff-only 2>/dev/null || { echo "$M_principale_aggiorna_remoto_2" >&2; exit 1; } collisione=$(comm -12 <(git -C "$principale" diff --name-only | sort -u) <(git diff --name-only "$(git merge-base HEAD "$(git -C "$principale" rev-parse HEAD)")" HEAD | sort -u)) [ -z "$collisione" ] || { echo "$M_merge_toccherebbe_file" >&2; echo "$collisione" | head -10 >&2; exit 1; } git -C "$principale" merge -q --no-edit "$ramo" || { git -C "$principale" merge --abort 2>/dev/null; ramo_principale=$(git -C "$principale" rev-parse --abbrev-ref HEAD); _f "$M_merge_non_pulito" ramo "$ramo_principale" >&2; exit 1; } if [ -z "$remoto" ]; then _f "$M_principale_senza_remoto" ramo "$ramo" else git -C "$principale" push -q origin HEAD || { echo "$M_mergiato_locale_push" >&2; exit 1; } _f "$M_principale_remoto" ramo "$ramo" fi ;; smonta) [ -d "$wt" ] || { _f "$M_nessun_worktree" wt "$wt"; exit 0; } sporchi=$(git -C "$wt" status --porcelain 2>/dev/null) [ -z "$sporchi" ] || { _f "$M_file_committati_smonta" wt "$wt" >&2; exit 1; } git -C "$principale" merge-base --is-ancestor "$ramo" HEAD \ || { _f "$M_principale_prima_merge" ramo "$ramo" >&2; exit 1; } git -C "$principale" worktree remove "$wt" && git -C "$principale" branch -q -d "$ramo" && _f "$M_smontato_tolto" wt "$wt" ramo "$ramo" ;; esac ;; esac