-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpullrepos
executable file
·76 lines (68 loc) · 2.41 KB
/
pullrepos
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
#!/usr/bin/env bash
#######################################################################
# Made by : Ewoud Dronkert
# Licence : GNU GPL v3
# Platform : macOS, Linux, WSL
# Requires : bash, git, curl, cut, sed, xargs
# Location : ~/bin/
# Name : pullrepos
# Version : 1.0.2
# Date : 2021-03-01
# Purpose : Update GitHub repositories. Clone if not found locally.
# Parameters : none
# Settings : GITPAT = GitHub personal access token with read access
# to the user and all their repos, set as environment var.
# Create yours at: https://github.com/settings/tokens
# : BASE = local directory where all repos are saved.
# Exit 0 : No errors
# 1 : GITPAT environment variable not set
# 2 : Local git directory not found
#######################################################################
if [ -z "$GITPAT" ]; then
echo "GitHub personal access token not set." >&2
echo "Get it at https://github.com/settings/tokens and" >&2
echo "set in ~/.profile: export GITPAT=tokengoeshere" >&2
exit 1
fi
BASE=~/git
if [ ! -e "$BASE" -o ! -d "$BASE" -o ! -r "$BASE" -o ! -w "$BASE" -o ! -x "$BASE" ]; then
echo "Local git directory not found or not accessible: $BASE" >&2
exit 2
fi
echo "Git directory : $BASE"
LOGIN=$(curl -s -H "Authorization: bearer $GITPAT" -X POST -d "{\"query\":\"{viewer{login}}\"}" https://api.github.com/graphql | cut -d'"' -f8)
PROFILE="https://github.com/$LOGIN"
CLONE="https://${GITPAT}@github.com/$LOGIN"
REPOS=$(curl -s -H "Authorization: bearer $GITPAT" -X POST -d "{\"query\":\"{viewer{repositories(first:100,orderBy:{field:NAME,direction:ASC}){nodes{name}}}}\"}" https://api.github.com/graphql | cut -d'[' -f2 | sed 'y/,/\n/' | cut -d'"' -f4 | xargs)
echo "GitHub profile : $PROFILE"
echo "Repositories : $REPOS"
echo
read -p "Update or clone all repositories? (Y)es (N)o " -n 1 -r
echo
if [[ ! "$REPLY" =~ ^[yYjJ]$ ]]; then
echo 'Bye.'
exit 0
fi
echo
cd "$BASE"
for repo in $REPOS; do
echo "##### $repo #####"
if [ -e "$repo" ]; then
if [ -d "$repo" -a -r "$repo" -a -w "$repo" -a -x "$repo" ]; then
if [ -d "${repo}/.git" ]; then
cd "$repo"
# git remote -v update -p
git pull -v --all --stat
cd ..
else
echo "Not a git repository: ${BASE}/${repo}" >&2
fi
else
echo "Not a directory or not accessible: ${BASE}/${repo}" >&2
fi
else
git clone "${CLONE}/${repo}.git"
fi
echo
done
exit 0