-
Notifications
You must be signed in to change notification settings - Fork 0
/
github-repo-cloner.sh
executable file
·77 lines (66 loc) · 2.04 KB
/
github-repo-cloner.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
# Parse the command-line arguments
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-u|--username)
github_username="$2"
shift
shift
;;
-t|--token)
github_token="$2"
shift
shift
;;
*)
echo "Unknown argument: $1"
exit 1
;;
esac
done
# Check that the username and token have been provided
if [ -z "$github_username" ] || [ -z "$github_token" ]; then
echo "Usage: $0 -u <GitHub username> -t <GitHub API token>"
exit 1
fi
# Set the file path for the list of repository URLs
url_list_file="repos.txt"
# Set the destination folder path
dest_folder="$(pwd)/repos"
# Create the destination folder if it doesn't exist
mkdir -p "$dest_folder"
# Get the list of repositories for the user
repositories=$(curl -s -H "Authorization: token $github_token" "https://api.github.com/user/repos?per_page=1000")
# Clone or update each repository listed in the response
for repo_url in $(echo "$repositories" | grep -Eo '"clone_url": "([^"]+)"' | awk '{print $2}' | tr -d '"'); do
# Extract the name of the repository from the URL
repo_name="$(basename "$repo_url" .git)"
echo "$repo_name"
# Check if the repository has already been cloned
if [ -d "$dest_folder/$repo_name" ]; then
# Update the repository
cd "$dest_folder/$repo_name"
git pull
cd -
else
# Clone the repository
git clone "$repo_url" "$dest_folder/$repo_name"
fi
done
# Read each URL from the URL list file and clone or update the corresponding repository
for url in $(cat "$url_list_file"); do
# Extract the name of the repository from the URL
repo_name="$(basename "$url" .git)"
echo "$repo_name"
# Check if the repository has already been cloned
if [ -d "$dest_folder/$repo_name" ]; then
# Update the repository
cd "$dest_folder/$repo_name"
git pull
cd -
else
# Clone the repository
git clone "$url" "$dest_folder/$repo_name"
fi
done