generated from PaulHBartley/BlankPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkrepo
executable file
·132 lines (104 loc) · 5.42 KB
/
mkrepo
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
function mkrepo() {
####################################################################################################
# OVERVIEW
# This function wraps $ git init <directory> with a script giving the option to automatically
# connect <directory> with a remote repository on GitHub.
#
# OPERATION
# $ mkrepo -r <directory> sends a call to the GitHub API to create a new remote repository (called
# <directory>) from a pre-defined template. The script then Git-initializes a new or existing local
# <directory> and links it with the newly-created remote repository.
#
# $ mkrepo <directory> simply initializes <directory> (new or existing) as the root of a local Git
# repository without connecting to the GitHub API.
#
# For the script to work, Curl and Git need to be installed, and the following variables need to be
# set in the active shell environment.
#
# GITHUB_USERNAME
# GITHUB_TOKEN
# GITHUB_TEMPLATE_REPO
#
# DIRECTORY BEHAVIOR
# $ mkrepo [-r] <directory> -- makes <directory> (new or existing) the root of a Git repository
# inside the current working directory.
# $ mkrepo [-r] <path>/<directory> -- makes <directory> (new or existing) the root of a Git
# repository inside the <path> destination directory.
# $ mkrepo [-r] -- makes the current working directory the root of a Git repository.
#
####################################################################################################
i=0
for arg in $1
do let i=i+1
done
j=0
for arg in $2
do let j=j+1
done
if [[ $# == 1 ]] && [[ $1 != -r ]] && [[ $i == 1 ]]; then
git init `dirname $1`/`basename $1`
elif [[ $# == 2 ]] && [[ $1 == -r ]] && [[ $j == 1 ]]; then
curl -s \
-i -u ${GITHUB_USERNAME}:${GITHUB_TOKEN} https://api.github.com/user \
-X POST \
-H 'Accept: application/vnd.github.baptiste-preview+json' \
"https://api.github.com/repos/${GITHUB_USERNAME}/${GITHUB_TEMPLATE_REPO}/generate" \
-d '{"name": "'"$(basename "$(echo "$2" | sed -e 's/ /-/g')")"'", "description": "New repo"}' &&
git init $(dirname "$2")/$(basename "$(echo "$2" | sed -e 's/ /-/g')") &&
cd $(dirname "$2")/$(basename "$(echo "$2" | sed -e 's/ /-/g')") &&
git remote add origin "https://github.com/${GITHUB_USERNAME}/$(basename "$(echo "$2" | sed -e 's/ /-/g')")" &&
git remote -v &&
sleep 2 &&
git pull origin main &&
cd $(dirname "$PWD")
elif [[ $# == 1 ]] && [[ $1 != -r ]] && [[ $i > 1 ]]; then
git init $(dirname "$*")/$(basename "$(echo "$*" | sed -e 's/ /-/g')")
elif [[ $# == 2 ]] && [[ $1 == -r ]] && [[ $j > 1 ]]; then
curl -s \
-i -u ${GITHUB_USERNAME}:${GITHUB_TOKEN} https://api.github.com/user \
-X POST \
-H 'Accept: application/vnd.github.baptiste-preview+json' \
"https://api.github.com/repos/${GITHUB_USERNAME}/${GITHUB_TEMPLATE_REPO}/generate" \
-d '{"name": "'"$(basename "$(echo "${*:2:$j}" | sed -e 's/ /-/g')")"'", "description": "New repo"}' &&
git init $(dirname "$2")/$(basename "$(echo "${*:2:$j}" | sed -e 's/ /-/g')") &&
cd $(dirname "$2")/$(basename "$(echo "${*:2:$j}" | sed -e 's/ /-/g')") &&
git remote add origin "https://github.com/${GITHUB_USERNAME}/$(basename "$(echo "${*:2:$j}" | sed -e 's/ /-/g')")" &&
git remote -v &&
sleep 2 &&
git pull origin main &&
cd $(dirname "$PWD")
elif [[ $# > 1 ]] && [[ $1 != -r ]] && [[ $i == 1 ]]; then
git init $(dirname "$*")/$(basename "$(echo "$*" | sed -e 's/ /-/g')")
elif [[ $# > 1 ]] && [[ $1 == -r ]] && [[ $j == 1 ]]; then
curl -s \
-i -u ${GITHUB_USERNAME}:${GITHUB_TOKEN} https://api.github.com/user \
-X POST \
-H 'Accept: application/vnd.github.baptiste-preview+json' \
"https://api.github.com/repos/${GITHUB_USERNAME}/${GITHUB_TEMPLATE_REPO}/generate" \
-d '{"name": "'"$(basename "$(echo "${*:2:$#}" | sed -e 's/ /-/g')")"'", "description": "New repo"}' &&
git init $(dirname "$2")/$(basename "$(echo "${*:2:$#}" | sed -e 's/ /-/g')") &&
cd $(dirname "$2")/$(basename "$(echo "${*:2:$#}" | sed -e 's/ /-/g')") &&
git remote add origin "https://github.com/${GITHUB_USERNAME}/$(basename "$(echo "${*:2:$#}" | sed -e 's/ /-/g')")" &&
git remote -v &&
sleep 2 &&
git pull origin main &&
cd $(dirname "$PWD")
elif [[ $# == 0 ]] && [[ $i == 0 ]] && [[ $j == 0 ]]; then
git init "$PWD"
elif [[ $# == 1 ]] && [[ $1 == -r ]] && [[ $i == 1 ]] && [[ $j == 0 ]]; then
curl -s \
-i -u ${GITHUB_USERNAME}:${GITHUB_TOKEN} https://api.github.com/user \
-X POST \
-H 'Accept: application/vnd.github.baptiste-preview+json' \
"https://api.github.com/repos/${GITHUB_USERNAME}/${GITHUB_TEMPLATE_REPO}/generate" \
-d '{"name": "'"$(basename "$(echo "$PWD")")"'", "description": "New repo"}' &&
git init "$PWD" &&
git remote add origin "https://github.com/${GITHUB_USERNAME}/$(basename "$(echo "$PWD")")" &&
git remote -v &&
sleep 2 &&
git pull origin main
else
echo "The command could not operate on the given directory name. Please try again."
exit 2
fi
}