-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaboli.sh
More file actions
executable file
·170 lines (137 loc) · 3.09 KB
/
Copy pathaboli.sh
File metadata and controls
executable file
·170 lines (137 loc) · 3.09 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/bin/bash
set -euo pipefail
shopt -s lastpipe
declare -r GITHUB_API_BASEURI="https://api.github.com"
declare -r SHAMEFUL_BRANCH=master
export $(grep -v '^#' .env | xargs)
main()
{
check-default-branch
check-git-template-dir
git-template "$GIT_TEMPLATE_DIR"
check-git-home-dir
git-local-scrub "$GIT_HOME_DIR"
check-dependencies
check-user
check-token
github-master-no-more
}
die()
{
echo >&2 "$1"
exit "$2"
}
check-non-empty-env()
{
if (( $# < 1 ))
then
die 'No environment name provided to check' 3
fi
if (( 2 == $# ))
then
local -r help=": $2"
else
local -r help=''
fi
local -r name="$1"
local error_message=''
if [[ ! -v "$name" || -z "${!name}" ]]
then
printf 'Please define variable '\''%b'\'' in .env%b' "$name" "$help" |
read error_message
die "$error_message" 2
fi
}
check-git-template-dir()
{
check-non-empty-env GIT_TEMPLATE_DIR 'template git dir'
GIT_TEMPLATE_DIR="$(eval echo "$( eval echo "$GIT_TEMPLATE_DIR" )")"
[ -d "$GIT_TEMPLATE_DIR" ] \
|| die "Variable GIT_TEMPLATE_DIR '$GIT_TEMPLATE_DIR' is not a directory" 2
}
check-git-home-dir()
{
check-non-empty-env GIT_HOME_DIR 'local directory hosting your Git repository'
GIT_HOME_DIR="$(eval echo "$( eval echo "$GIT_HOME_DIR" )")"
[ -d "$GIT_HOME_DIR" ] \
|| die "Variable GIT_HOME_DIR '$GIT_HOME_DIR' is not a directory" 2
}
check-dependencies()
{
for exe in jq curl
do
type $exe >/dev/null 2>&1 \
|| die "Please install '$exe'." 1
done
}
check-default-branch()
{
check-non-empty-env DEFAULT_BRANCH 'new main branch name (main, trunk)'
}
check-user()
{
check-non-empty-env GITHUB_USER 'Github username'
}
check-token()
{
check-non-empty-env GITHUB_TOKEN 'Github user token (visit https://github.com/settings/tokens)'
}
github-call()
{
local -r path="${1#/}"
shift
declare -ar curl_args=(
-k
-u "$GITHUB_USER:$GITHUB_TOKEN"
-H 'Accept: application/vnd.github.v3+json'
-H 'Content-type: application/json'
"$GITHUB_API_BASEURI/$path"
"$@"
)
curl ${curl_args[@]} ||
die "Error on API call" 4
}
github-master-no-more()
{
local -r repos="$(github-call "/users/$GITHUB_USER/repos" | jq -r '.[] | .name')"
local repo
for repo in $repos
do
local refs="$(github-call \
"/repos/$GITHUB_USER/$repo/git/refs")"
local sha=$(echo $refs | jq -r '.[]|select(.ref=="refs/heads/'$SHAMEFUL_BRANCH'")|.object.sha')
github-call \
"/repos/$GITHUB_USER/$repo/git/refs" \
-X POST \
-d '{"ref": "refs/heads/'$DEFAULT_BRANCH'", "sha": "'$sha'"}'
github-call \
"/repos/$GITHUB_USER/$repo" \
-X PATCH \
-d '{ "default_branch": "'$DEFAULT_BRANCH'"}'
github-call "/repos/$GITHUB_USER/$repo/git/refs/heads/$SHAMEFUL_BRANCH" -X DELETE
done
}
git-template()
{
local -r target="$1"
shift
git init --bare $target
echo ref: "refs/heads/$DEFAULT_BRANCH" > "$target/HEAD"
git config --global init.templateDir "$target"
}
git-local-scrub()
{
local path="$1"
shift
for repo in $(find "$path" -mindepth 1 -maxdepth 1 -type d)
do
cd "$repo"
if ! git switch "$DEFAULT_BRANCH"
then
git checkout -b "$DEFAULT_BRANCH"
git branch -D "$SHAMEFUL_BRANCH"
fi
cd -
done
}
main