defmain(): # 获取所有仓库名称 file_list = [] for f in os.listdir(directory): if f in exclude: continue file_list.append(f) logging.info(f"找到仓库共计:{len(file_list)}个") logging.info(f"排除仓库:{len(exclude)}个 {exclude}") logging.info(file_list) ifnot os.path.exists(temp_path): os.makedirs(temp_path) logging.info(f"文件夹 {temp_path} 创建成功") c = os.listdir(temp_path) if c: logging.error(f"error:目录{temp_path}非空") return count = 0 for name in file_list: count+=1 p = temp_path+"/"+name u = origin_url+'/'+name logging.info(f'scan repo({count}/{len(file_list)}): {u}') ifnot os.path.exists(p): os.makedirs(p) else: logging.error(f"error:目录{p}已存在") return Repo.clone_from( url=u, to_path=p ) #获取远程分支的分支名称 repo = git.Repo(p) remote_branches = [] for ref in repo.git.branch('-r').split('\n'): remote_branches.append(ref) #格式化分支名称 del remote_branches[0] bran_name = [] pattern = r"origin/(.*)" for bran in remote_branches: match = re.search(pattern, bran) if match: bran_name.append( match.group(1)) else: logging.error("No match found.") return logging.info(f"远程分支:{bran_name}") # 在本地切换一遍分支,因为在上传至新的gitlab库时,只会把已存在的本地分支上传,没有的不会上传,所以必须把所有分支都切换一遍,把远程分支下载到本地。 for bran in bran_name: logging.info(f"check 分支:{bran}") repo.git.checkout(bran)