• 核心就是使用中间文件,因为没有办法直接修改源文件
  • 注意此程序会检索当前文件下的所有文件,并进行冲突拆解
  • git冲突文件复杂,这里使用了一个一个变量来记录当前游标的状态,只有游标为指定值得时候,才会对读取到的当前行进行写入
import os


def new_file(file_name):
    a = 0
    base_file = open(file_name, 'r', encoding='utf8')  # 源文件
    new_file = open('{}.new'.format(file_name), 'w', encoding='utf8')  # 新文件
    for line in base_file:
        if '<<<<<<<' in line:
            a = 1
            continue
        elif line.startswith('======='):
            a = 0
            continue
        else:
            if a != 0:
                continue
            else:
                if '>>>>>>>' in line:
                    continue
                else:
                    new_file.write(line)

    base_file.close()
    new_file.close()


def old_file(file_name):
    base_file = open(file_name, 'r', encoding='utf8')  # 源文件
    old_file = open('{}.old'.format(file_name), 'w', encoding='utf8')  # 新文件
    c = 0
    for line in base_file:
        if '<<<<<<<' in line:
            continue
        elif line.startswith('======='):
            c = 1
            continue
        elif '>>>>>>>' in line:
            c = 0
            continue
        else:
            if c == 0:
                old_file.write(line)

    base_file.close()
    old_file.close()


def get_file():
    file_path = os.getcwd()
    return os.listdir(file_path)


def main():
    for file_name in get_file():
        try:
            new_file(file_name)
            old_file(file_name)
        except Exception as e:
            print('{}不支持的操作'.format(e))


if __name__ == '__main__':
    main()
Last modification:April 26th, 2020 at 11:00 am
安安,亲觉得有用, 请随意打赏嗷