I tried to ask deep seek for a solution. As usual, it didn't work, it never works. But it gave me a script that I could debug (at least something to start with). In the end, this version seems to work for me:
#!/bin/bash
# Ensure we are in the root of the Git repository
if [ ! -d .git ]; then
echo "This script must be run from the root of a Git repository."
exit 1
fi
# First commit
COMMIT0=$(git rev-list --max-parents=0 HEAD)
# Get the list of all commits in the master branch in chronological order
COMMITS=$(git rev-list --reverse master)
# Create a new branch for the rewritten history
NEW_BRANCH="fixed-line-endings"
git checkout -b "$NEW_BRANCH" "$COMMIT0"
# Iterate over each commit
for COMMIT in $COMMITS; do
if [ "$COMMIT" != "$COMMIT0" ]; then
git cherry-pick -X theirs "$COMMIT"
fi
# Convert line endings for .cpp, .c, .h, *.hpp, *.txt files to LF
find . -type f \( -name '*.cpp' -o -name '*.c' -o -name '*.h' -o -name '*.hpp' -o -name '*.txt' \) -exec dos2unix -q {} \;
# Stage the changes
git add -u
# Amend the commit with the changes
git commit --amend --no-edit --allow-empty
done
# Switch back to the master branch
git checkout master
echo "Line endings have been fixed in the new branch '$NEW_BRANCH'."
Perhaps, it's possible to make it less chatty to reduce all the logging from git command. Any suggestions/iprovements?