Commit 8ed455d62ce2

Vincent Demeester <vincent@sbr.pm>
2026-02-24 11:28:49
fix(nix-flake-update): delete branch after worktree removal
Moved branch cleanup into the EXIT trap so it runs after the worktree is removed, preventing 'cannot delete branch used by worktree' errors.
1 parent adf6203
Changed files (1)
tools
nix-flake-update
tools/nix-flake-update/nix-flake-update.sh
@@ -100,6 +100,13 @@ cleanup() {
     [[ -n "$WORKTREE_DIR" ]] && rm -rf "$WORKTREE_DIR" || true
   fi
 
+  # Clean up the update branch (must happen after worktree removal)
+  if [ -n "${BRANCH_NAME:-}" ] && git show-ref --verify --quiet "refs/heads/$BRANCH_NAME" 2>/dev/null; then
+    log "Cleaning up branch: $BRANCH_NAME"
+    cd "$REPO_PATH"
+    git branch -D "$BRANCH_NAME" 2>&1 | tee -a "$LOG_FILE" || true
+  fi
+
   if [ $exit_code -ne 0 ]; then
     log "ERROR: Update process failed with exit code $exit_code"
     
@@ -274,9 +281,6 @@ Auto-merge: $AUTO_MERGE"
       "Builds failed for updated $input_desc. TODO added to inbox. Check logs: $LOG_FILE" \
       "x,flake,warning"
 
-    # Clean up failed branch in main repo
-    cd "$REPO_PATH"
-    git branch -D "$BRANCH_NAME" 2>&1 | tee -a "$LOG_FILE" || true
     exit 1
   fi
 
@@ -285,10 +289,6 @@ else
   notify "low" "ℹ️ No Flake Updates" \
     "flake.lock is already up to date" \
     "information_source,flake"
-
-  # Clean up unused branch in main repo
-  cd "$REPO_PATH"
-  git branch -D "$BRANCH_NAME" 2>&1 | tee -a "$LOG_FILE" || true
 fi
 
 log "Flake update process complete"