mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-29 14:30:52 +00:00
fix(worktree): pick only slots whose subnets are free, up to 254
worktree_next_slot skipped only slots pinned in a worktree's custom.env, so it handed out slot 255 (an invalid 172.30.255.0/24 bind range) once the low slots were taken, and it reused a slot whose 172.30.<slot>.0/24 or 10.160.<slot>.0/24 subnet a stack without a checkout still held. It now also skips slots whose subnets a docker network uses, and up.sh refuses to pin past WORKTREE_MAX_SLOT=254 instead of writing an invalid slot. 🎯 Fix: 92 % 💥 Break: 3 % 💸 Cost: – ⏱️ Time: – 🛡️ Security: 100 % 🏆 Score: 89 🚦 Gate: ✅ 90/5, cost and time <= 100, security 100 % Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
feaf9120ed
commit
5d912e1eda
4 files changed
+143
-13
No files matched your search
@@ -57,15 +57,48 @@ worktree_slot_of() {
|
||||
printf '%s' "${slot}"
|
||||
}
|
||||
|
||||
WORKTREE_MAX_SLOT=254
|
||||
WORKTREE_NET_PREFIX=172.30
|
||||
WORKTREE_LAB_PREFIX=10.160
|
||||
|
||||
worktree_used_subnets() {
|
||||
local -a ids=()
|
||||
command -v docker >/dev/null 2>&1 || return 0
|
||||
mapfile -t ids < <(docker network ls -q 2>/dev/null)
|
||||
[ "${#ids[@]}" -gt 0 ] || return 0
|
||||
docker network inspect "${ids[@]}" \
|
||||
--format '{{range .IPAM.Config}}{{.Subnet}}{{"\n"}}{{end}}' 2>/dev/null || return 0
|
||||
}
|
||||
|
||||
# Param slot: the candidate slot.
|
||||
# Param subnets: newline-separated subnets docker networks already hold.
|
||||
# Returns: 0 when a network holds the slot's compose or swarm lab subnet.
|
||||
worktree_slot_taken() {
|
||||
local subnet
|
||||
while read -r subnet; do
|
||||
case "${subnet}" in
|
||||
"${WORKTREE_NET_PREFIX}.$1."* | "${WORKTREE_LAB_PREFIX}.$1."*) return 0 ;;
|
||||
esac
|
||||
done <<<"$2"
|
||||
return 1
|
||||
}
|
||||
|
||||
worktree_next_slot() {
|
||||
local max=0 path slot
|
||||
local used=" " path slot subnets
|
||||
while read -r path; do
|
||||
slot="$(worktree_slot_of "${path}")"
|
||||
if [ "${slot}" -gt "${max}" ]; then
|
||||
max="${slot}"
|
||||
fi
|
||||
used+="$(worktree_slot_of "${path}") "
|
||||
done < <(git worktree list --porcelain | awk '/^worktree /{print $2}')
|
||||
printf '%s' "$((max + 1))"
|
||||
subnets="$(worktree_used_subnets)"
|
||||
for ((slot = 1; slot <= WORKTREE_MAX_SLOT; slot++)); do
|
||||
case "${used}" in
|
||||
*" ${slot} "*) continue ;;
|
||||
esac
|
||||
if ! worktree_slot_taken "${slot}" "${subnets}"; then
|
||||
printf '%s' "${slot}"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
printf '%s' "$((WORKTREE_MAX_SLOT + 1))"
|
||||
}
|
||||
|
||||
worktree_meta_dir() {
|
||||
|
||||
@@ -18,7 +18,6 @@ source "${SCRIPT_DIR}/lib.sh"
|
||||
|
||||
branch="${1:?branch name required}"
|
||||
base="${2:-}"
|
||||
max_slot=255
|
||||
|
||||
cd "${REPO_ROOT}"
|
||||
|
||||
@@ -38,8 +37,8 @@ if [ -e "${path}" ]; then
|
||||
fi
|
||||
|
||||
slot="$(worktree_next_slot)"
|
||||
if [ "${slot}" -gt "${max_slot}" ]; then
|
||||
echo "FAILURE: no free slot left (max ${max_slot}); tear down an existing worktree" >&2
|
||||
if [ "${slot}" -gt "${WORKTREE_MAX_SLOT}" ]; then
|
||||
echo "FAILURE: no free slot left (max ${WORKTREE_MAX_SLOT}); tear down an existing worktree" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
Reference in new issue
Block a user