Skip to content

Commit 0ccde04

Browse files
committed
refactor(klipper): optimize git command execution by eliminating directory changes
• Remove initial 'cd $KLIPPER_DIR' approach in checkout_target_branch() function • Replace with explicit '-C $KLIPPER_DIR' flags on all individual git commands • Update all git commands to use explicit directory specification: - git symbolic-ref HEAD - git checkout -b (temporary branch creation) - git show-ref --verify --quiet (branch existence check) - git checkout (existing branch switch) - git checkout -b (new branch from remote) - git branch -D (cleanup commands in both error and success paths) • Eliminate directory access validation at function start (no longer needed) Benefits: - Eliminates side effects: Working directory of calling context remains unchanged - Improves error recovery: ERR trap cleanup doesn't need to worry about directory state - Enhances code clarity: Each git command explicitly specifies its target directory - Reduces complexity: No need for directory access validation at function start - Better isolation: Function becomes more self-contained and predictable - Maintains all existing error handling and logging behavior - Preserves ERR trap cleanup mechanism for temporary branches Technical Implementation: - All git commands now use '-C $KLIPPER_DIR' for explicit directory context - Cleanup function updated to use explicit directory specification - Function behavior remains identical after the change - No changes to error codes, logging, or trap mechanisms
1 parent b921b93 commit 0ccde04

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

configuration/scripts/klipper-fork-migration.sh

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,6 @@ checkout_target_branch()
280280
{
281281
log_info "Checking out target branch..." "checkout_branch"
282282

283-
cd "$KLIPPER_DIR" || {
284-
log_error "Cannot change to Klipper directory" "checkout_branch" "KLIPPER_DIR_ACCESS_FAILED"
285-
return 1
286-
}
287-
288283
# Track if we created a temporary branch for cleanup
289284
local temp_branch=""
290285
local created_temp_branch=false
@@ -293,7 +288,7 @@ checkout_target_branch()
293288
cleanup_temp_branch_on_error() {
294289
if [ "$created_temp_branch" = true ] && [ -n "$temp_branch" ]; then
295290
log_info "Cleaning up temporary migration branch due to error: $temp_branch" "checkout_branch"
296-
if git branch -D "$temp_branch" >/dev/null 2>&1; then
291+
if git -C "$KLIPPER_DIR" branch -D "$temp_branch" >/dev/null 2>&1; then
297292
log_info "Successfully cleaned up temporary branch: $temp_branch" "checkout_branch" "GIT_TEMP_BRANCH_CLEANUP"
298293
else
299294
log_warn "Failed to clean up temporary branch: $temp_branch (this is not critical)" "checkout_branch" "GIT_TEMP_BRANCH_CLEANUP_FAILED"
@@ -306,11 +301,11 @@ checkout_target_branch()
306301
trap 'cleanup_temp_branch_on_error' ERR
307302

308303
# Check if we're in detached HEAD state
309-
if ! git symbolic-ref HEAD >/dev/null 2>&1; then
304+
if ! git -C "$KLIPPER_DIR" symbolic-ref HEAD >/dev/null 2>&1; then
310305
log_info "Repository is in detached HEAD state." "checkout_branch"
311306
log_info "Creating and checking out a temporary branch..." "checkout_branch"
312307
temp_branch="temp-migration-$(date +%s)-$$"
313-
if ! execute_with_logging git checkout -b "$temp_branch" "checkout_branch" "GIT_TEMP_BRANCH_FAILED"; then
308+
if ! execute_with_logging git -C "$KLIPPER_DIR" checkout -b "$temp_branch" "checkout_branch" "GIT_TEMP_BRANCH_FAILED"; then
314309
log_error "Failed to create temporary branch" "checkout_branch" "GIT_TEMP_BRANCH_FAILED"
315310
cleanup_temp_branch_on_error
316311
return 1
@@ -319,16 +314,16 @@ checkout_target_branch()
319314
fi
320315

321316
# Check if target branch already exists locally
322-
if git show-ref --verify --quiet "refs/heads/$TARGET_BRANCH"; then
317+
if git -C "$KLIPPER_DIR" show-ref --verify --quiet "refs/heads/$TARGET_BRANCH"; then
323318
log_info "Local branch '$TARGET_BRANCH' already exists, switching to it..." "checkout_branch"
324-
if ! execute_with_logging git checkout "$TARGET_BRANCH" "checkout_branch" "GIT_CHECKOUT_FAILED"; then
319+
if ! execute_with_logging git -C "$KLIPPER_DIR" checkout "$TARGET_BRANCH" "checkout_branch" "GIT_CHECKOUT_FAILED"; then
325320
log_error "Failed to checkout existing branch '$TARGET_BRANCH'" "checkout_branch" "GIT_CHECKOUT_FAILED"
326321
cleanup_temp_branch_on_error
327322
return 1
328323
fi
329324
else
330325
log_info "Creating and checking out branch '$TARGET_BRANCH' from RatOS fork..." "checkout_branch"
331-
if ! execute_with_logging git checkout -b "$TARGET_BRANCH" "$RATOS_FORK_REMOTE/$TARGET_BRANCH" "checkout_branch" "GIT_CHECKOUT_REMOTE_FAILED"; then
326+
if ! execute_with_logging git -C "$KLIPPER_DIR" checkout -b "$TARGET_BRANCH" "$RATOS_FORK_REMOTE/$TARGET_BRANCH" "checkout_branch" "GIT_CHECKOUT_REMOTE_FAILED"; then
332327
log_error "Failed to checkout branch '$TARGET_BRANCH' from RatOS fork" "checkout_branch" "GIT_CHECKOUT_REMOTE_FAILED"
333328
log_error "Please ensure the branch exists on the remote repository." "checkout_branch" "GIT_CHECKOUT_REMOTE_FAILED"
334329
cleanup_temp_branch_on_error
@@ -339,7 +334,7 @@ checkout_target_branch()
339334
# Clean up temporary branch if we created one (successful completion)
340335
if [ "$created_temp_branch" = true ] && [ -n "$temp_branch" ]; then
341336
log_info "Cleaning up temporary migration branch: $temp_branch" "checkout_branch"
342-
if execute_with_logging git branch -D "$temp_branch" "checkout_branch" "GIT_TEMP_BRANCH_CLEANUP"; then
337+
if execute_with_logging git -C "$KLIPPER_DIR" branch -D "$temp_branch" "checkout_branch" "GIT_TEMP_BRANCH_CLEANUP"; then
343338
log_info "Successfully cleaned up temporary branch: $temp_branch" "checkout_branch"
344339
else
345340
log_warn "Failed to clean up temporary branch: $temp_branch (this is not critical)" "checkout_branch" "GIT_TEMP_BRANCH_CLEANUP_FAILED"

0 commit comments

Comments
 (0)