import subprocess, os, sys
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
base = 'D:\\Code\\DTU 程序\\STM32F4-Base\\'

result = subprocess.run(
    ['C:\\Program Files\\Git\\bin\\git.exe', 'show', '3466c4c:Drivers/BSP/CH395F/ch395f.c'],
    capture_output=True,
    cwd=base
)
git_text = result.stdout.decode('utf-8', errors='replace')

with open(os.path.join(base, 'Drivers\\BSP\\CH395F\\ch395f.c'), 'rb') as f:
    wt_text = f.read().decode('utf-8', errors='replace')

rep = chr(0xfffd)
print('Git version: {} FFFD chars'.format(git_text.count(rep)))
print('Working tree: {} FFFD chars'.format(wt_text.count(rep)))

git_lines = git_text.split(chr(10))
wt_lines = wt_text.split(chr(10))
for i in range(min(len(git_lines), len(wt_lines))):
    if git_lines[i] != wt_lines[i]:
        print('\nFirst diff at line {}:'.format(i+1))
        print('  GIT: {}'.format(git_lines[i][:120]))
        print('  WT:  {}'.format(wt_lines[i][:120]))
        hex_g = git_lines[i].encode('utf-8').hex()[:40]
        hex_w = wt_lines[i].encode('utf-8').hex()[:40]
        print('  GIT hex: {}'.format(hex_g))
        print('  WT hex:  {}'.format(hex_w))
        break
else:
    print('Files are identical')
