-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathsanitize_git_filenames.sh
More file actions
46 lines (32 loc) · 1.32 KB
/
Copy pathsanitize_git_filenames.sh
File metadata and controls
46 lines (32 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
sanitize_git_filenames() {
local repo_root
repo_root=$(git rev-parse --show-toplevel 2>/dev/null) || {
echo "Not inside a Git repository."
return 1
}
# Characters forbidden on Windows
local forbidden='[<>:"/\\|?*]'
local replacement="_"
# Crawl through repo
find "$repo_root" -depth -name '*[<>:"/\\|?*]*' | while IFS= read -r file; do
dir=$(dirname "$file")
base=$(basename "$file")
# Generate new safe filename
safe=$(echo "$base" | sed "s/$forbidden/$replacement/g")
# If it changed, rename
if [[ "$base" != "$safe" ]]; then
echo "Renaming: $file -> $dir/$safe"
git mv -f "$file" "$dir/$safe"
fi
done
}
# How it works:
# Uses git rev-parse --show-toplevel to find the repo root.
# find … -depth ensures we rename files before directories (avoids breaking paths).
# sed "s/$forbidden/$replacement/g" replaces all forbidden characters with _.
# Uses git mv -f to rename so Git tracks the move.
# Example usage:
# cd /path/to/repo
# sanitize_git_filenames
# This will scan the repo and automatically rename anything invalid on Windows.
# Do you want me to also handle reserved names like CON, NUL, COM1 etc. (which are valid in Linux but not in Windows), or just forbidden characters?