import os.path import shutil from git import Repo from scripts.task import Task from utils import FileUtils from utils.logger_utils import app_logger def clean_workspace(repo): # 1. 放弃工作区所有已跟踪文件的修改(包括恢复被删除的已跟踪文件) # - checkout 命令会将工作区文件重置为当前 HEAD 版本的状态 repo.git.checkout('.') # 2. 删除工作区所有未跟踪的文件和目录(包括空目录) # - -f:强制删除(避免询问) # - -d:同时删除未跟踪的目录 repo.git.clean('-fd') print("工作空间已清空,恢复到当前分支最新提交状态") class ProjectCopy(Task): def execute(self): if os.path.exists(self.context.temp_project_path): temp_path = os.path.join(self.context.temp_project_path, "build", "outputs") if os.path.exists(temp_path): shutil.rmtree(temp_path) temp_path = os.path.join(self.context.temp_project_path, "build", "generated") if os.path.exists(temp_path): shutil.rmtree(temp_path) app_logger().debug("project '{}' to '{}'".format(self.context.temp_project_path, "项目已经存在了")) repo = Repo(self.context.temp_project_path) temp_repo_commit = repo.head.commit.hexsha[:10] app_logger().debug( f"project '{self.context.temp_project_path}' , local '{temp_repo_commit}' remote '{self.context.local_repo_commit}") if temp_repo_commit != self.context.local_repo_commit: app_logger().debug("本地代码有变动,需要更新临时的目录") # 本地分支和远程的分支不一样 clean_workspace(repo) origin = repo.remote("origin") origin.fetch() repo.git.checkout(self.context.local_repo_commit) self.context.update_code = True self.context.update_config = True self.context.update_keystore = True self.context.update_res_img = True self.context.update_res_icon = True self.context.update_res_unity = True else: self.context.update_code = False app_logger().debug("本地的代码没有变动,无需更新") return self.context.update_code = True result = FileUtils.copy(self.context.project_original_path, self.context.temp_project_path) app_logger().debug("Copied project '{}' to '{}'".format(self.context.project_original_path, result)) pass