Update fix_paths.sh
This commit is contained in:
@@ -1,119 +1,80 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env bash
|
||||||
"""
|
# -----------------------------------------------------------------------------
|
||||||
fix_paths.py
|
# Copyright (C) 2025 Moko Consulting <hello@mokoconsulting.tech>
|
||||||
|
#
|
||||||
|
# This file is part of a Moko Consulting project.
|
||||||
|
#
|
||||||
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
# the terms of the GNU General Public License as published by the Free Software
|
||||||
|
# Foundation; either version 3 of the License, or (at your option) any later
|
||||||
|
# version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License (./LICENSE.md).
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
Normalizes invalid Windows-style backslash separators in repository *paths*.
|
# FILE INFORMATION
|
||||||
|
# DEFGROUP: MokoStandards
|
||||||
|
# INGROUP: Generic.Script
|
||||||
|
# REPO: https://github.com/mokoconsulting-tech/MokoStandards
|
||||||
|
# PATH: /scripts/fix_paths.sh
|
||||||
|
# VERSION: 01.00.00
|
||||||
|
# BRIEF: Replace Windows-style path separators with POSIX separators in text files.
|
||||||
|
|
||||||
What it does
|
# =============================================================================
|
||||||
- Uses `git ls-files` as the authoritative inventory of tracked paths.
|
# fix_paths.sh
|
||||||
- Detects any tracked path that contains a backslash (\\).
|
#
|
||||||
- Renames the path to a forward-slash (/) equivalent via `git mv`.
|
# Purpose:
|
||||||
- Fails fast on collisions (when the normalized target path already exists).
|
# - Normalize path separators in text files to forward slashes (/).
|
||||||
|
# - Intended for CI validation and optional remediation workflows.
|
||||||
|
# - Skips binary files and version control metadata.
|
||||||
|
# - Preserves file contents aside from path separator normalization.
|
||||||
|
#
|
||||||
|
# Usage:
|
||||||
|
# ./scripts/fix_paths.sh
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
What it does NOT do
|
set -euo pipefail
|
||||||
- Does not rewrite file contents.
|
|
||||||
- Does not alter untracked files.
|
|
||||||
|
|
||||||
Intended usage
|
ROOT_DIR="${1:-.}"
|
||||||
- Called by CI (GitHub Actions) and locally.
|
|
||||||
- Safe to run repeatedly (idempotent when no invalid paths exist).
|
|
||||||
|
|
||||||
Exit codes
|
info() {
|
||||||
- 0: Success, no invalid paths or all renames completed
|
echo "INFO: $*"
|
||||||
- 1: Operational error (git failure, collision, or unexpected exception)
|
}
|
||||||
"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
warn() {
|
||||||
|
echo "WARN: $*" 1>&2
|
||||||
|
}
|
||||||
|
|
||||||
import os
|
die() {
|
||||||
import subprocess
|
echo "ERROR: $*" 1>&2
|
||||||
import sys
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
command -v find >/dev/null 2>&1 || die "find not available"
|
||||||
|
command -v sed >/dev/null 2>&1 || die "sed not available"
|
||||||
|
command -v file >/dev/null 2>&1 || die "file not available"
|
||||||
|
|
||||||
def run(cmd: list[str]) -> subprocess.CompletedProcess:
|
info "Scanning for text files under: $ROOT_DIR"
|
||||||
return subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
|
||||||
|
|
||||||
|
while IFS= read -r -d '' file; do
|
||||||
|
if file "$file" | grep -qi "text"; then
|
||||||
|
if grep -q '\\\\' "$file"; then
|
||||||
|
sed -i.bak 's#\\\\#/#g' "$file" && rm -f "$file.bak"
|
||||||
|
info "Normalized paths in $file"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < <(
|
||||||
|
find "$ROOT_DIR" \
|
||||||
|
-type f \
|
||||||
|
-not -path "*/.git/*" \
|
||||||
|
-not -path "*/node_modules/*" \
|
||||||
|
-print0
|
||||||
|
)
|
||||||
|
|
||||||
def ensure_repo_root() -> None:
|
info "Path normalization complete."
|
||||||
# In CI we usually start at the repo root, but this enforces determinism.
|
|
||||||
workspace = os.environ.get("GITHUB_WORKSPACE")
|
|
||||||
if workspace and os.path.isdir(workspace):
|
|
||||||
os.chdir(workspace)
|
|
||||||
|
|
||||||
|
|
||||||
def require_git_repo() -> None:
|
|
||||||
p = run(["git", "rev-parse", "--is-inside-work-tree"])
|
|
||||||
if p.returncode != 0 or p.stdout.strip() != "true":
|
|
||||||
print("Error: not inside a git work tree", file=sys.stderr)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
def list_tracked_paths() -> list[str]:
|
|
||||||
p = run(["git", "ls-files"])
|
|
||||||
if p.returncode != 0:
|
|
||||||
print("Error: git ls-files failed", file=sys.stderr)
|
|
||||||
print(p.stderr, file=sys.stderr)
|
|
||||||
sys.exit(1)
|
|
||||||
return [line for line in p.stdout.splitlines() if line.strip()]
|
|
||||||
|
|
||||||
|
|
||||||
def path_exists(path: str) -> bool:
|
|
||||||
# Use git to evaluate existence of a tracked path when possible.
|
|
||||||
# For collision detection we use filesystem existence because the target may not be tracked yet.
|
|
||||||
return os.path.exists(path)
|
|
||||||
|
|
||||||
|
|
||||||
def normalize_path(p: str) -> str:
|
|
||||||
return p.replace("\\", "/")
|
|
||||||
|
|
||||||
|
|
||||||
def git_mv(old: str, new: str) -> None:
|
|
||||||
p = run(["git", "mv", "-f", old, new])
|
|
||||||
if p.returncode != 0:
|
|
||||||
print(f"Error: git mv failed for {old} -> {new}", file=sys.stderr)
|
|
||||||
print(p.stderr, file=sys.stderr)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
def main() -> int:
|
|
||||||
ensure_repo_root()
|
|
||||||
require_git_repo()
|
|
||||||
|
|
||||||
tracked = list_tracked_paths()
|
|
||||||
bad = [p for p in tracked if "\\" in p]
|
|
||||||
|
|
||||||
if not bad:
|
|
||||||
print("No invalid backslash separators detected in tracked paths")
|
|
||||||
return 0
|
|
||||||
|
|
||||||
print(f"Detected {len(bad)} invalid tracked path(s). Normalizing.")
|
|
||||||
|
|
||||||
# Sort longest-first to reduce rename issues in nested scenarios.
|
|
||||||
bad.sort(key=len, reverse=True)
|
|
||||||
|
|
||||||
for old in bad:
|
|
||||||
new = normalize_path(old)
|
|
||||||
|
|
||||||
if old == new:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Collision check: if target exists and is not the same logical file.
|
|
||||||
if path_exists(new):
|
|
||||||
print("Collision detected. Aborting.", file=sys.stderr)
|
|
||||||
print(f"Source: {old}", file=sys.stderr)
|
|
||||||
print(f"Target: {new}", file=sys.stderr)
|
|
||||||
return 1
|
|
||||||
|
|
||||||
# Ensure destination directory exists.
|
|
||||||
dest_dir = os.path.dirname(new)
|
|
||||||
if dest_dir:
|
|
||||||
os.makedirs(dest_dir, exist_ok=True)
|
|
||||||
|
|
||||||
git_mv(old, new)
|
|
||||||
print(f"Renamed: {old} -> {new}")
|
|
||||||
|
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
sys.exit(main())
|
|
||||||
|
|||||||
Reference in New Issue
Block a user