DEV Community

Mikhail Nefedov
Mikhail Nefedov

Posted on

Easing the start at a new company with glab or gh

Easing the start at a new company with glab or gh

When I started at my fist software engineering job, I made my life harder than it had to be. The team already had many microservices and a lot of (NuGet) packages. They were organized neatly using GitLab's subgroups feature. This made it easier to find specific packages grouped by a domain specific usage.

Unfortunately, I started to clone those packages whenever my work required updating them. I'll be honest - this led to chaos in my local file system. In the end, I had two folders (microservices and NuGet packages) containing many repositories. Sometimes the subfolders followed the structure on GitLab. In most cases, they did not. It was quite messy and disorganized.

That's when I decided to clean it up and tried to find a way of organizing my folders quickly.

glab for gitlab

Gitlab has an official cli tool called glab. We can use this tool to clone all active repositories of a specific GitLab group via:

glab repo clone -g mygitlabgroup --include-subgroups --a=false --paginate
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • glab repo clone Command to clone a repository
  • -g mygitlabgroup Group (mygitlabgroup) from which repositories will be cloned
  • --include-subgroups All repos within sub-groups of the group (mygitlabgroup) will be cloned
  • -a=false Ignore archived repos
  • --paginate All projects will be fetched

The last argument is important, because from my understanding, glab uses the GitLab API. Since the underlying response is paginated, it's very possible that we would miss a lot of repositories.

You can find more options for the git repo clone command here: https://gitlab.com/gitlab-org/cli/-/blob/main/docs/source/repo/clone.md

gh for GitHub

I wanted to look at another big platform that I use in my free time. GitHub does have a CLI-tool called gh.

Unfortunately, there is no option to clone all repositories of an organization. You can use a bash script for cloning:

#!bin/bash

repos=$(gh repo list organization --no-archived --json url --limit 999 | grep -oP '"url":"\K[^"]+')

for repo in $repos; do
    echo $repo
    gh repo clone $repo
done
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • gh repo list List repositories owned by user/organization
  • --no-archived Exclude archived repositories
  • --json url Only return the repository url
  • --limit 999 Maximum count of repos in response
  • grep -oP '"url":"\K[^"]+' Extract url
  • gh repo clone $repo Clone repository

Other solutions were discussed in this Stack Overflow thread: https://stackoverflow.com/questions/46725688/how-to-clone-all-repos-including-private-repos-from-github

End

I hope this helps you get off to a faster start at your new job. It also can make the process of migrating to a new development laptop smoother by reducing the time spent on manual cloning and keeping things organized.

Top comments (0)