Skip to content

Commit e2be0e3

Browse files
committed
refactor(klipper-migration): optimize uncommitted changes detection with single git operation
- Replace dual git operations (git diff --cached + git diff) with single git status --porcelain - Maintain identical error detection behavior for staged and unstaged changes - Preserve detailed error messages listing affected files - Improve performance by reducing git subprocess calls from two to one - Enhance maintainability with cleaner, more readable parsing logic - Keep same error codes: KLIPPER_STAGED_CHANGES, KLIPPER_UNCOMMITTED_CHANGES
1 parent 8cda3cf commit e2be0e3

File tree

1 file changed

+41
-17
lines changed

1 file changed

+41
-17
lines changed

configuration/scripts/klipper-fork-migration.sh

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -108,24 +108,48 @@ check_uncommitted_changes()
108108
return 1
109109
}
110110

111-
# Check for staged changes
112-
if ! git diff --cached --quiet; then
113-
log_error "There are staged changes in the Klipper repository." "check_changes" "KLIPPER_STAGED_CHANGES"
114-
log_error "Please commit or stash these changes before running migration." "check_changes" "KLIPPER_STAGED_CHANGES"
115-
local staged_files
116-
staged_files=$(git diff --cached --name-only)
117-
log_error "Staged files: $staged_files" "check_changes" "KLIPPER_STAGED_CHANGES"
118-
return 1
119-
fi
111+
# Use git status --porcelain to check for both staged and unstaged changes in one operation
112+
# --untracked-files=no ignores untracked files (matching previous behavior)
113+
local status_output
114+
status_output=$(git status --porcelain --untracked-files=no)
115+
116+
if [ -n "$status_output" ]; then
117+
# Parse the status output to categorize changes
118+
local staged_files=""
119+
local modified_files=""
120+
121+
while IFS= read -r line; do
122+
if [ -n "$line" ]; then
123+
local status_code="${line:0:2}"
124+
local file_path="${line:3}"
125+
126+
# Check if file has staged changes (first character is not space)
127+
if [[ "${status_code:0:1}" != " " ]]; then
128+
staged_files="${staged_files}${file_path}\n"
129+
fi
130+
131+
# Check if file has unstaged changes (second character is not space)
132+
if [[ "${status_code:1:1}" != " " ]]; then
133+
modified_files="${modified_files}${file_path}\n"
134+
fi
135+
fi
136+
done <<< "$status_output"
120137

121-
# Check for unstaged changes (ignoring untracked files)
122-
if ! git diff --quiet; then
123-
log_error "There are uncommitted changes in the Klipper repository." "check_changes" "KLIPPER_UNCOMMITTED_CHANGES"
124-
log_error "Please commit or stash these changes before running migration." "check_changes" "KLIPPER_UNCOMMITTED_CHANGES"
125-
local modified_files
126-
modified_files=$(git diff --name-only)
127-
log_error "Modified files: $modified_files" "check_changes" "KLIPPER_UNCOMMITTED_CHANGES"
128-
return 1
138+
# Report staged changes if any
139+
if [ -n "$staged_files" ]; then
140+
log_error "There are staged changes in the Klipper repository." "check_changes" "KLIPPER_STAGED_CHANGES"
141+
log_error "Please commit or stash these changes before running migration." "check_changes" "KLIPPER_STAGED_CHANGES"
142+
log_error "Staged files: $(echo -e "$staged_files" | tr '\n' ' ')" "check_changes" "KLIPPER_STAGED_CHANGES"
143+
return 1
144+
fi
145+
146+
# Report unstaged changes if any
147+
if [ -n "$modified_files" ]; then
148+
log_error "There are uncommitted changes in the Klipper repository." "check_changes" "KLIPPER_UNCOMMITTED_CHANGES"
149+
log_error "Please commit or stash these changes before running migration." "check_changes" "KLIPPER_UNCOMMITTED_CHANGES"
150+
log_error "Modified files: $(echo -e "$modified_files" | tr '\n' ' ')" "check_changes" "KLIPPER_UNCOMMITTED_CHANGES"
151+
return 1
152+
fi
129153
fi
130154

131155
log_info "No uncommitted changes found." "check_changes"

0 commit comments

Comments
 (0)