首先拿到需要比较版本的git 的 id
vscode可以在时间线找
xxx是你的id,旧版本
head是 新版本
然后在终端执行
git diff xxx head --name-only > diff.txt
执行python导出
import os
import shutil
import chardet
# 定义源目录和目标目录
source_dir = 'D:/work/cqsp-service-web' # 替换为你的源目录路径
destination_dir = 'C:/Users/86150/Desktop/cqsp-service-web' # 替换为你的目标目录路径
# 定义要复制的文件列表
# 初始化文件列表
files_to_copy = []
# 使用原始字符串指定diff.txt文件的完整路径
diff_file_path = 'D:/work/cqsp-service-web/diff.txt'
try:
# 检测文件编码
with open(diff_file_path, 'rb') as f:
raw_data = f.read()
result = chardet.detect(raw_data)
encoding = result['encoding']
# 打开并读取diff.txt文件
with open(diff_file_path, 'r', encoding=encoding) as file:
# 逐行读取文件内容
for line in file:
# 去除每行的首尾空白字符(包括换行符)
file_path = line.strip()
# 将文件路径添加到列表中
files_to_copy.append(file_path)
# 打印结果,查看files_to_copy列表内容
print(files_to_copy)
except FileNotFoundError:
print(f"文件未找到,请检查路径:{diff_file_path}")
except Exception as e:
print(f"发生错误:{e}")
# 如果目标目录不存在,则创建它
if not os.path.exists(destination_dir):
os.makedirs(destination_dir)
# 遍历文件列表,复制每个文件到目标目录
for file_name in files_to_copy:
full_file_name = os.path.join(source_dir, file_name) # 构建源文件的完整路径
destination_file_name = os.path.join(destination_dir, file_name) # 构建目标文件的完整路径
# 获取目标文件的目录
destination_file_dir = os.path.dirname(destination_file_name)
# 确保目标文件的目录存在,如果不存在,则创建它
if not os.path.exists(destination_file_dir):
os.makedirs(destination_file_dir)
# 确保源文件存在
if os.path.isfile(full_file_name):
shutil.copy(full_file_name, destination_file_name) # 复制文件
print(f"Copied {file_name} to {destination_dir}")
else:
print(f"File {file_name} not found in {source_dir}. Skipping...")
print("File copying complete.")