I ended up calling the projects directly and using their respective namespaces to create the pathToFolder. So rather than cd'ing in and out just create the absolute paths.
import os
import gitlab
from git import Repo
GITLAB_TOKEN = os.getenv('GITLAB_TOKEN')
gl = gitlab.Gitlab(url='https://gitlab.example.priv', private_token=GITLAB_TOKEN)
gitBasePathRelative = "repos/"
gitBasePathRelativeAbsolut = os.path.expanduser("~/" + gitBasePathRelative)
os.makedirs(gitBasePathRelativeAbsolut,exist_ok=True)
for p in gl.projects.list(all=True):
print("Cloning project " + p.name)
pathToFolder = p.namespace['name'] + "/" + p.name
if not os.path.exists(pathToFolder):
print("\tCreating directory for project " + pathToFolder)
os.makedirs(pathToFolder)
Repo.clone_from(p.ssh_url_to_repo, pathToFolder)
Answer from user3063045 on Stack Overflow
» pip install gitlab-clone
Update Dec. 2022, use glab repo clone
glab repo clone -g <group> -a=false -p --paginate
With:
-p,--preserve-namespace: Clone the repo in a subdirectory based on namespace--paginate: Make additional HTTP requests to fetch all pages of projects before cloning. Respects--per-page-a,--archived: Limit by archived status. Use with-a=falseto exclude archived repositories. Used with--groupflag
That does support cloning more than 100 repositories (since MR 1030, and glab v1.24.0, Dec. 2022)
This is for gitlab.com or for a self-managed GitLab instance, provided you set the environment variable GITLAB_URI or GITLAB_HOST: it specifies the URL of the GitLab server if self-managed (eg: https://gitlab.example.com).
Original answer and updates (starting March 2015):
Not really, unless:
you have a 21st project which references the other 20 as submodules.
(in which case a clone followed by agit submodule update --initwould be enough to get all 20 projects cloned and checked out)or you somehow list the projects you have access (GitLab API for projects), and loop on that result to clone each one (meaning that can be scripted, and then executed as "one" command)
Since 2015, Jay Gabez mentions in the comments (August 2019) the tool gabrie30/ghorg
ghorgallows you to quickly clone all of an org's or user's repos into a single directory.
Usage:
$ ghorg clone someorg
$ ghorg clone someuser --clone-type=user --protocol=ssh --branch=develop
$ ghorg clone gitlab-org --scm=gitlab --namespace=gitlab-org/security-products
$ ghorg clone --help
Also (2020): https://github.com/ezbz/gitlabber
usage: gitlabber [-h] [-t token] [-u url] [--debug] [-p]
[--print-format {json,yaml,tree}] [-i csv] [-x csv]
[--version]
[dest]
Gitlabber - clones or pulls entire groups/projects tree from gitlab
One liner with curl, jq, tr:
Copyfor repo in $(curl -s --header "PRIVATE-TOKEN: your_private_token" https://<your-host>/api/v4/groups/<group_id> | jq -r ".projects[].ssh_url_to_repo"); do git clone $repo; done;
For Gitlab.com use https://gitlab.com/api/v4/groups/<group_id>
To include subgroups add include_subgroups=true query param like
https://<your-host>/api/v4/groups/<group_id>?include_subgroups=true
Note: To clone with http url use
http_url_to_repoinstead ofssh_url_to_repoin jq (Thanks @MattVon for the comment)
» pip install gitlabber
It looks like there's not a straightforward solution for HTTPS-based cloning regarding GitLab. Therefore if you want a SSH-based cloning, you should take account these three forthcoming steps:
Create properly an SSH key using your email used to sign up. I would use the default filename to key for Windows. Don't forget to introduce a password! (tip: you can skip this step if you already have one ssh key here)
$ ssh-keygen -t rsa -C "[email protected]" -b 4096 Generating public/private rsa key pair. Enter file in which to save the key ($PWD/.ssh/id_rsa): [\n] Enter passphrase (empty for no passphrase):[your password] Enter same passphrase again: [your password] Your identification has been saved in $PWD/.ssh/id_rsa. Your public key has been saved in $PWD/.ssh/id_rsa.pub.Copy and paste all content from the recently
id_rsa.pubgenerated into Setting>SSH keys>Key from your GitLab profile.# Copy to clipboard pbcopy < ~/.ssh/id_rsa.pubGet locally connected:
$ ssh -i $PWD/.ssh/id_rsa [email protected] Enter passphrase for key "$PWD/.ssh/id_rsa": [your password] PTY allocation request failed on channel 0 Welcome to GitLab, you! Connection to gitlab.com closed.
Finally, clone any private or internal GitLab repository!
$ git clone https://git.metabarcoding.org/obitools/ROBIBarcodes.git
Cloning into 'ROBIBarcodes'...
remote: Counting objects: 69, done.
remote: Compressing objects: 100% (65/65), done.
remote: Total 69 (delta 14), reused 0 (delta 0)
Unpacking objects: 100% (69/69), done.
You have your ssh clone statement wrong: git clone username [email protected]:root/test.git
That statement would try to clone a repository named username into the location relative to your current path, [email protected]:root/test.git.
You want to leave out username:
git clone [email protected]:root/test.git