-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirror.sh
executable file
·55 lines (46 loc) · 2.02 KB
/
mirror.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env bash
# Readme for this script:
#
# This script will clone all the repositories from a GitHub organization.
# It uses gh, a GitHub CLI tool, and jq, a JSON parser.
# You can install it with nix: nix shell nixpkgs#gitAndTools.gh nixpkgs#jq. Run `shell.sh` to do that.
# But this script comes with flake.nix, so you can just run it with direnv with direnv allow.
# Usage information and help.
usage() {
echo "Usage: $0 [organization] [limit]"
echo " organization: The name of the organization to fetch the repositories from. If not provided, it will be 'doma-engineering'."
echo " limit: The amount of the repositories to fetch. If not provided, it will be the amount of repositories the organization has."
echo "Example: $0 doma-engineering 10"
}
# If first argument is -h or --help, print usage information and exit.
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
usage
exit 0
fi
# Check if we're logged into GitHub with gh.
if ! gh auth status; then
gh auth login
fi
#### LET'S GO ####
# First argument is the name of the organization. If not provided, it will be "doma-engineering".
ORG=${1:-doma-engineering}
# Get all the repositories from the organization.
REPOS="$(gh api "https://api.github.com/orgs/$ORG/repos" | jq '.[].name')"
REPOS_AMOUNT="$(wc -l <<< "$REPOS")"
# Second argument is the amount of the repositories to fetch. If not provided, it will be the amount of repositories the organization has.
LIMIT=${2:-$REPOS_AMOUNT}
# Clone all repos from a GitHub organization.
gh repo list "$ORG" --limit "$LIMIT" | while read -r repo _; do
# Check if the repo isn't cloned yet. Note that $repo has $ORG as the prefix.
if [[ -d "$repo" ]]; then
echo "$repo is already cloned. Pulling the latest changes."
cd "$repo" || exit 1
# Pull the latest changes.
git pull
cd - || exit 1
else
# Else clone it into "$repo" with gh.
# It uses `clone "$repo" "$repo"` to trick gh into cloning the repo into a super-directory with the same name as organisation.
gh repo clone "$repo" "$repo"
fi
done