Last active
December 31, 2023 11:41
-
-
Save 99702/97df85c28803a56d333a7a4a453964ec to your computer and use it in GitHub Desktop.
Clones all repositories of private / gitlab into local machine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
import subprocess | |
from tqdm import tqdm | |
import os | |
# user inputs | |
private_token = "your-private-token" | |
clone_dir="/home/kali/Documents/gitlabs" | |
your_domain = "gitlab.example.com" | |
domain = f"https://pat:{private_token}@{your_domain}" | |
# start git alias clone-branches | |
command = """ | |
git config --global alias.clone-branches "! git branch -a | sed -n \"/\\/HEAD /d; /\\/master$/d; /remotes/p;\n#\" | xargs -L1 git checkout -t" | |
""" | |
alias_command = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) | |
# end git alias clone-branches | |
r = requests.get(f'{domain}/api/v4/projects?private_token={private_token}&per_page=500') | |
gits = [] | |
for rr in r.json(): | |
son = rr.get('http_url_to_repo') | |
gits.append(son) | |
if not os.path.exists(clone_dir): | |
os.makedirs(clone_dir) | |
for url in tqdm(gits): | |
repo_name = url.split("/")[-1].replace(".git", "") | |
clone_url_with_token = f"{url[:-4]}.git" | |
git_command = ["git", "clone", domain + "/" + url[29:], f"{clone_dir}/{repo_name}"] | |
subprocess.run(git_command) | |
if os.path.exists(f"{clone_dir}/{repo_name}"): | |
os.chdir(f"{clone_dir}/{repo_name}") | |
fetch_command = ["git", "clone-branches"] | |
subprocess.run(fetch_command) | |
print("Repositories cloned successfully.") | |
print("==================== REPOSITORIES ARE AVAILABLE IN = " , clone_dir, " ====================") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment