Git messed up my files, showing chinese characters in some places

后端 未结 4 922
野的像风
野的像风 2021-02-01 04:44

disclaimer: By Git, I mean \'I\' messed up.

Earlier, I wanted git-gui to show me the diff for which it thinks are binary files.

<
4条回答
  •  南笙
    南笙 (楼主)
    2021-02-01 05:43

    Here's a (bad) power-shell script that will fix files in this state. It will replace the sequence "0x0D 0x00 0x0D 0x0A" with "0x0D 0x00 0x0A" then overwrite the file it was given.

    Afterwards you should probably re-save the file in something like UTF-8.

    function Fix-Encoding
    {
        Param(
            [String]$file
        )
        $f = get-item $file;
        $bytes = [System.IO.File]::ReadAllBytes($f.fullname);
        $output = new-object "System.Collections.Generic.List[System.Byte]"
        $output.Capacity = $bytes.Length
    
        for ($i = 0; $i -lt $bytes.Length; $i++)
        { 
            if ($i -lt $bytes.Length + 3)
            {
                if ($bytes[$i] -eq 0x0D -and $bytes[$i+1] -eq 0x00 -and $bytes[$i+2] -eq 0x0D -and $bytes[$i+3] -eq 0x0A) 
                {
                    $output.Add(0x0D);
                    $output.Add(0x00);
                    $output.Add(0x0A);
                    $i += 3
                }
                else {
                    $output.Add($bytes[$i]);
                }
            }
         }
        [System.IO.File]::WriteAllBytes($f.fullname, $output)
    }
    

提交回复
热议问题