Cloning All Your GitHub Repositories or Updating Them in One Command
Wednesday 12 October 2022

ruby

I had a new laptop couple weeks ago at work, and I had to set it up with everything I need including the projects I have been working on from GitHub, I remembered how I usually git clone every project I start working on, it involves going to GitHub to the project page, copy the clone URL then I go to my terminal, cd to the projects directory and executes git clone http://github.com/org/repo.

But what if I don’t need to do that, What if I just cloned all the repositories at once and update them all with one command, that could save a lot of time when I have a new machine or I came back from a long vacation and I want to update all the machines.

The implementation is pretty trivial, I should connect to GitHub API and get the list of repositories for me or the organization I want to clone, iterate over every repo and clone or update it, and because GitHub doesn’t return all repositories in one page I have to repeat that process until I get an empty page.

The following snippet does that, it needs some dependencies like tty-prompt and colorize both can be removed, but I like to have colored text and menus for that script.

 1#!/usr/bin/env ruby
 2require 'tty-prompt'
 3require 'open-uri'
 4require 'json'
 5require 'colorize'
 6require 'colorized_string'
 7
 8prompt = TTY::Prompt.new
 9
10user_name = prompt.ask("What is name of the user or organisation?")
11type = prompt.select("What type is it?", user: "users", organisation: "orgs")
12token = ENV['GITHUB_API_TOKEN']
13
14unless token
15  puts "Can't find GITHUB_API_TOKEN in you environment!".light_red
16  puts "Use the follow URL to generate a token: https://github.com/settings/tokens".light_red
17  puts "Then add it to your .bashrc/.bash_profile/...etc as a variable GITHUB_API_TOKEN".light_red
18  exit
19end
20
21page = -1
22loop do
23  page += 1
24  url = "https://api.github.com/#{type}/#{user_name}/repos?access_token=#{token}&page=#{page}"
25  repos = open(url).read
26  repos = JSON.load(repos)
27  break if repos.empty?
28
29  repos.each do |repo|
30    if Dir.exist?(repo["name"])
31      puts "Updating #{repo["name"]}...".light_blue
32      Dir.chdir(repo["name"]) do
33        system("git pull --all")
34      end
35    else
36      puts "Cloning #{repo["name"]}...".light_blue
37      system("git clone #{repo["ssh_url"]}")
38    end
39  end
40end

Backlinks

See Also