import time from git import Repo, RemoteProgress from .task import Task def progress(op_code, cur_count, max_count=None, message=''): if op_code == RemoteProgress.END: print() print(f"操作: {op_code}, 进度: {cur_count}/{max_count}, 消息: {message}") class ProjectInit(Task): def execute(self): try: repo = Repo(self.context.project_original_path) except Exception: repo = Repo.clone_from(self.context.repo_url, self.context.project_original_path, recursive=True, progress=progress) # for submodule in repo.submodules: # print(f"子模块 '{submodule.name}' 路径: {submodule.path}") # # print(f"Commit ID: {submodule.module().head.commit}") if self.context.repo_commit: pass elif self.context.repo_branch: # 克隆仓库 branch_name = self.context.repo_branch remote_name = "origin" # 远程仓库默认名称 repo.git.fetch(remote_name) # 2. 创建本地分支并跟踪远程分支 remote_branch_ref = f"{remote_name}/{branch_name}" local_branch = repo.create_head(branch_name, remote_branch_ref) # 创建本地分支指向远程 local_branch.set_tracking_branch(repo.remotes[remote_name].refs[branch_name]) # 设置跟踪 local_branch.checkout() # 切换到该分支 self.context.local_repo_branch = repo.active_branch.name self.context.local_repo_commit = repo.head.commit.hexsha[:10] # 拉取最新代码 repo.remotes.origin.pull() pass else: raise Exception(f"No commit to {self.context.repo_commit}") repo.git.submodule('update', '--init', '--recursive') for submodule in repo.submodules: print(submodule.url) print(submodule.name) print(submodule.hexsha) print(submodule.path) sub_repo = submodule.module() sub_repo.git.reset("--hard", submodule.hexsha) print(f"Reset {submodule.name} to {submodule.hexsha[:7]}")