| 
									
										
										
										
											2025-07-07 03:27:26 +00:00
										 |  |  | 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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-08 03:09:50 +00:00
										 |  |  |             if repo.active_branch.name != branch_name: | 
					
						
							| 
									
										
										
										
											2025-08-01 07:48:16 +00:00
										 |  |  |                 # 检查本地是否已存在该分支 | 
					
						
							|  |  |  |                 if branch_name in repo.heads: | 
					
						
							|  |  |  |                     # 本地分支已存在,直接切换 | 
					
						
							|  |  |  |                     repo.heads[branch_name].checkout() | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     # 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()  # 切换到该分支 | 
					
						
							| 
									
										
										
										
											2025-07-07 03:27:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             self.context.local_repo_branch = repo.active_branch.name | 
					
						
							|  |  |  |             self.context.local_repo_commit = repo.head.commit.hexsha[:10] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # 拉取最新代码 | 
					
						
							|  |  |  |             repo.remotes.origin.pull() | 
					
						
							| 
									
										
										
										
											2025-08-01 02:14:00 +00:00
										 |  |  |             print("当前分支:" + repo.active_branch.name) | 
					
						
							| 
									
										
										
										
											2025-07-07 03:27:26 +00:00
										 |  |  |             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]}") |