-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitman-github
More file actions
executable file
·115 lines (93 loc) · 2.68 KB
/
Copy pathgitman-github
File metadata and controls
executable file
·115 lines (93 loc) · 2.68 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
#!/bin/bash
# by Dominik Stanisław Suchora <hexderm@gmail.com>
# License: GNU GPLv3
declare -r arg0="$(basename "$0")" reg_repos_delim=$'\a' reg_repos_delim_lit='\a' reg_repos_delim_2='"' reg_repos_delim_3=$'\t' reg_remotes_delim=$'\a'
declare -r f_repo_path=1 f_repo_description=2 f_repo_tags=3 f_repo_remotes=4 f_repo_firstcommit=5 f_repo_lastcommit=6 f_repo_lastchange=7
[ -z "$GITMAN_GITHUB_USER" ] && {
echo "GITMAN_GITHUB_USER env var is not set" >&2
exit 1
}
reg_repos_field() {
local fields="$1"
[ -z "$fields" ] && fields="1-"
cut -d "$reg_repos_delim" -f "$fields"
}
reg_repo_path() {
reg_repos_field "$f_repo_path"
}
repo_exists() {
curl -s -D - "https://api.github.com/repos/$GITMAN_GITHUB_USER/$1" | grep -q '^HTTP/[^ ]* 404$'
}
comm_add() {
local rname="$1" url="$2" data="$3"
[ "$#" -lt 3 ] && exit 1
local path="$(reg_repo_path <<< "$data")"
gh repo create --public "$(basename "$path")"
}
to_api_url_conv() {
sed -E 's#^git@github.com:([^/]+)/([^/]+)\.git$#https://api.github.com/repos/\1/\2#' <<< "$1"
}
get_repo_topics() {
local url="$(to_api_url_conv "$1")"
curl -s "$url/topics" | jq -r '.names[]' | paste -sd ,
}
comm_update() {
local rname="$1" url="$2" data="$3"
[ "$#" -lt 3 ] && exit 1
if git ls-remote "$url" &>/dev/null
then
true
else
echo "coudn't connect to remote \"$url\"" >&2
exit 1
fi
gh repo edit --remove-topic "$(get_repo_topics "$url")" "$url"
gh repo edit --add-topic "$(reg_repos_field "$f_repo_tags" <<< "$data" | tr "$reg_repos_delim_2" ',' | sed 's/,$//;s/^,//')" \
--description "$(reg_repos_field "$f_repo_description" <<< "$data")" "$url"
}
comm_move() {
local rname="$1" url="$2" data="$3" repo_name="$4"
[ "$#" -lt 4 ] && exit 1
gh repo rename -R "$url" "$repo_name" || exit 1
local path="$(reg_repo_path <<< "$data")"
git -C "$path" remote set-url "$rname" "$(comm_url "" "$repo_name")"
}
comm_delete() {
local rname="$1" url="$2" data="$3"
[ "$#" -lt 3 ] && exit 1
gh repo delete "$url"
}
comm_url() {
local rname="$1" repo_name="$2"
[ "$#" -lt 2 ] && exit 1
echo "git@github.com:$GITMAN_GITHUB_USER/$repo_name.git"
}
[ "$#" -lt 1 ] && exit 1
if [ "$1" = "--test" ]
then
[ "$#" -lt 2 ] && exit 1
case "$2" in
add) true;;
update) true;;
move) true;;
delete) true;;
url) true;;
*) exit 1;;
esac
exit 0
fi
declare comm="$1"
shift
case "$comm" in
add)
comm_add "$@";;
update)
comm_update "$@";;
move)
comm_move "$@";;
delete)
comm_delete "$@";;
url)
comm_url "$@";;
*) exit 1;;
esac