getAllGithubBranch.py
#!/usr/bin/env python
# encoding: utf-8
import os
import subprocess
def export_git_branches_to_folders(local_repo_path, output_dir):
"""
Clones all branches from a local git repository and exports them to separate folders.
:param local_repo_path: The path to the local git repository.
:param output_dir: The path to the output directory where branch folders will be created.
"""
# 确保输出目录存在
os.makedirs(output_dir, exist_ok=True)
# 切换到Git仓库目录
os.chdir(local_repo_path)
# 获取所有远程分支列表
branches = subprocess.check_output(['git', 'branch', '-r']).decode().splitlines()
# 处理每个远程分支
for branch in branches:
branch = branch.strip()
if '->' in branch:
# 跳过引用头指针
continue
# 分支名格式化:去除"origin/"并将斜杠替换为下划线
branch_name = branch.replace('origin/', '').replace('/', '_')
# 创建分支对应的输出目录
branch_dir = os.path.join(output_dir, branch_name)
os.makedirs(branch_dir, exist_ok=True)
# 使用git archive将分支内容导出到tar文件,然后解压到目标文件夹
subprocess.run(['git', 'archive', '--format=tar', branch, '-o', 'temp.tar'])
subprocess.run(['tar', '-xf', 'temp.tar', '-C', branch_dir])
os.remove('temp.tar')
print("All branches have been exported to folders.")
def clone_all_branches(repo_list_file, output_dir):
"""
Clone all branches from each repository listed in a txt file.
:param repo_list_file: The path to the txt file containing git repository URLs.
:param output_dir: The base directory to clone the repositories into.
"""
# 确保输出目录存在
os.makedirs(output_dir, exist_ok=True)
# 读取仓库列表文件
with open(repo_list_file, 'r') as file:
repos = file.readlines()
# 克隆每个仓库的所有分支
cloned_repos = []
for repo_url in repos:
repo_url = repo_url.strip()
if not repo_url:
continue
# 从仓库URL中提取仓库名
repo_name = os.path.basename(repo_url)
if repo_name.endswith('.git'):
repo_name = repo_name[:-4]
# 克隆目录路径
clone_dir = os.path.join(output_dir, repo_name)
# 克隆仓库
print(f'Cloning repository: {repo_url}')
subprocess.run(['git', 'clone', repo_url, clone_dir])
# 添加克隆的仓库路径到列表中
cloned_repos.append(os.path.abspath(clone_dir))
return cloned_repos
# 使用
# repo_list_file = txt文件路径
repo_list_file = 'github_url.txt'
# repo_dir = 仓库目录
repo_dir = '/repo'
# output_dir = 输出目录
output_dir = '/cloned_repositories'
cloned_repos_paths = clone_all_branches(repo_list_file, repo_dir)
for repo_path in cloned_repos_paths:
print(repo_path)
file_path = repo_path.split("/")[-1]
print("out: ", output_dir+f"/{file_path}")
repos = export_git_branches_to_folders(repo_path, output_dir+f"/{file_path}")
github_url.txt
https://github.com/xxx.git
https://github.com/xxx.git
https://github.com/xxx.git