Skip to content

Commit 9718010

Browse files
committed
feat(klipper-migration): add automatic cleanup for temporary migration branches
- Track creation of temporary branches during detached HEAD state handling - Implement automatic cleanup using git branch -D after successful checkout - Add comprehensive logging for cleanup operations with proper error codes - Handle cleanup failures gracefully with warnings instead of script failure - Prevent accumulation of orphaned temporary branches in repository - Use GIT_TEMP_BRANCH_CLEANUP and GIT_TEMP_BRANCH_CLEANUP_FAILED error codes - Maintain repository cleanliness throughout migration process
1 parent e2be0e3 commit 9718010

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

configuration/scripts/klipper-fork-migration.sh

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,16 +237,20 @@ checkout_target_branch()
237237
return 1
238238
}
239239

240+
# Track if we created a temporary branch for cleanup
241+
local temp_branch=""
242+
local created_temp_branch=false
243+
240244
# Check if we're in detached HEAD state
241245
if ! git symbolic-ref HEAD >/dev/null 2>&1; then
242246
log_info "Repository is in detached HEAD state." "checkout_branch"
243247
log_info "Creating and checking out a temporary branch..." "checkout_branch"
244-
local temp_branch
245248
temp_branch="temp-migration-$(date +%s)-$$"
246249
if ! execute_with_logging git checkout -b "$temp_branch" "checkout_branch" "GIT_TEMP_BRANCH_FAILED"; then
247250
log_error "Failed to create temporary branch" "checkout_branch" "GIT_TEMP_BRANCH_FAILED"
248251
return 1
249252
fi
253+
created_temp_branch=true
250254
fi
251255

252256
# Check if target branch already exists locally
@@ -265,6 +269,16 @@ checkout_target_branch()
265269
fi
266270
fi
267271

272+
# Clean up temporary branch if we created one
273+
if [ "$created_temp_branch" = true ] && [ -n "$temp_branch" ]; then
274+
log_info "Cleaning up temporary migration branch: $temp_branch" "checkout_branch"
275+
if execute_with_logging git branch -D "$temp_branch" "checkout_branch" "GIT_TEMP_BRANCH_CLEANUP"; then
276+
log_info "Successfully cleaned up temporary branch: $temp_branch" "checkout_branch"
277+
else
278+
log_warn "Failed to clean up temporary branch: $temp_branch (this is not critical)" "checkout_branch" "GIT_TEMP_BRANCH_CLEANUP_FAILED"
279+
fi
280+
fi
281+
268282
log_info "Successfully checked out branch '$TARGET_BRANCH'." "checkout_branch"
269283
return 0
270284
}

0 commit comments

Comments
 (0)