GitLab: checkout all available repositories

Generate a private token

https://<GITLAB-SERVER1>/profile/personal_access_tokens
https://<GITLAB-SERVER2>/profile/personal_access_tokens

Checkout a list of all available repositories

QUERY='.[] | .path_with_namespace + "\t" + .ssh_url_to_repo' # JQ Query
curl --request GET --header "PRIVATE-TOKEN: <PRIVATE-TOKEN>" "<GITLAB-SERVER1>/api/v4/projects?simple=true&per_page=65536" | jq -r $QUERY > repo.list
curl --request GET --header "PRIVATE-TOKEN: <PRIVATE-TOKEN>"" "<GITLAB-SERVER2>/api/v3/projects?simple=true&per_page=65536" | jq -r $QUERY >> repo.list

Create directories for repositories

cat repo.list | cut -f1 | xargs mkdir p-

Checkout projects (with GNU parallel)

parallel --colsep '\t' --jobs 4 -a repo.list git clone {2} {1}

Build list of git repositories

find -type d -name ".git"  | xargs realpath | xargs dirname > path.list  

Report repository branch or checkout branch

cat path.list | xargs -I{} sh -c "cd {}; echo {}; git branch"
cat path.list | xargs -I{} sh -c "cd {}; echo {}; git checkout master"
cat path.list | xargs -I{} sh -c "cd {}; echo {}; git checkout develop"

Note: when you are migrating repositoires you should use git clone --mirror

Update: try adding get all available repositories. if you don’t get all projects and just get 404 you’re fucked. Try creating the list from what you see browsing GitLab or try to get Admin-Access.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.