Preserve local changes during safe repository sync
Homelab Main / validate (push) Waiting to run Details
Homelab Main / running-update (push) Blocked by required conditions Details
Homelab Main / debian-ops (push) Blocked by required conditions Details

This commit is contained in:
juvenal.diaz 2026-11-04 10:21:55 -06:00
parent 78116413e6
commit b14a6bd0b6
1 changed files with 5 additions and 26 deletions

View File

@ -22,33 +22,12 @@ current_branch="$("${git_cmd[@]}" branch --show-current)"
echo "Git remote not found: ${remote}" >&2
exit 2
}
# OpenTofu leaves transient lock metadata after an interrupted plan. Remove
# only these generated files, and only when no OpenTofu process is running;
# real source changes remain blocking as before.
if ! pgrep -af '[t]ofu' >/dev/null 2>&1; then
while IFS= read -r stale_tofu_lock; do
[[ -n "${stale_tofu_lock}" ]] || continue
rm -f "${stale_tofu_lock}"
echo "Removed stale OpenTofu lock metadata: ${stale_tofu_lock}"
done < <(find "${repo_root}/bootstrap" -type f -name '.terraform.tfstate.lock.info' -print)
fi
if [[ -f "${repo_root}/go.sum" ]] && ! "${git_cmd[@]}" ls-files --error-unmatch go.sum >/dev/null 2>&1; then
rm -f "${repo_root}/go.sum"
echo "Removed generated untracked Go checksum file: ${repo_root}/go.sum"
fi
go_mod_diff="$("${git_cmd[@]}" diff --unified=0 -- go.mod)"
if [[ -n "${go_mod_diff}" && "${go_mod_diff}" == *"// indirect"* ]]; then
added_go_mod_lines="$(printf "%s\n" "${go_mod_diff}" | grep -E "^\\+[^+]" || true)"
non_generated_go_mod_lines="$(printf "%s\n" "${added_go_mod_lines}" | grep -vE "^\\+(require \\(|\\)|[[:space:]]+[^[:space:]]+[[:space:]]+v[^[:space:]]+[[:space:]]+// indirect|[[:space:]]*)$" || true)"
if [[ -z "${non_generated_go_mod_lines}" ]]; then
"${git_cmd[@]}" restore -- go.mod
echo "Restored generated indirect-only Go module changes."
fi
fi
# Git's fast-forward merge preserves unrelated local modifications and refuses
# to overwrite conflicting tracked or untracked files. Never delete local lock
# metadata or restore working files as a side effect of syncing.
if [[ -n "$("${git_cmd[@]}" status --porcelain)" ]]; then
echo "Refusing repo sync: working tree is not clean." >&2
"${git_cmd[@]}" status --short >&2
exit 1
echo "Local changes detected; retaining them and checking for conflicts during fast-forward."
"${git_cmd[@]}" status --short
fi
before="$("${git_cmd[@]}" rev-parse HEAD)"