-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-create.sh
executable file
·143 lines (121 loc) · 4.34 KB
/
git-create.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
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
#!/bin/sh
###############################################################################
#
# Author: Akim Sissaoui
# Website: https://akim.sissaoui.com/informatique
#
# Description:
# This script simplifies the creation of a GitHub repository.
# If not already present, it will create a "github" directory in the home folder.
# Subsequently, it will create a new directory based on the user-provided name,
# initialize it as a git repository, and create a corresponding GitHub repository.
# User can choose to create a private or public repository.
#
# Requirements:
# 1. SSH authentication to GitHub as described at:
# https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/
# 2. A GitHub API token generated from https://github.com/settings/tokens
# (with admin tree permissions) and stored in the ~/.github file.
#
# Usage:
# ./git-create.sh <repo_name> <repo_type>
#
# <repo_name> required: Repository name adhering to GitHub's naming convention.
# <repo_type> required: Can be "private" or "public".
#
# Examples:
# 1. Create a private GitHub repository named "MyApp":
# ./git-create.sh MyApp private
# 2. Create a public GitHub repository named "MyCreation":
# ./git-create.sh MyCreation public
#
###############################################################################
print_help() {
echo "Usage: ./git-create.sh <repo_name> <repo_type>"
echo
echo "<repo_name> required: Repository name adhering to GitHub's naming convention."
echo "<repo_type> required: Can be 'private' or 'public'."
echo
echo "Examples:"
echo "1. Create a private GitHub repository named 'MyApp':"
echo " ./git-create.sh MyApp private"
echo "2. Create a public GitHub repository named 'MyCreation':"
echo " ./git-create.sh MyCreation public"
}
pre_reqs() {
error_message=""
# Check if required parameters are provided
if [ -z "$reponame" ]
then
error_message+="Error: Repository name is required.\n"
fi
if [ -z "$repo_type" ]
then
error_message+="Error: Repository type is required.\n"
fi
# Check if the required files exist
if [ ! -f ~/.github ]
then
error_message+="Error: ~/.github file does not exist.\n"
fi
if [ ! -f ~/.ssh/github.key ]
then
error_message+="Error: ~/.ssh/github.key file does not exist.\n"
fi
# Test the ssh authentication
git_user=$(ssh -i ~/.ssh/github.key -T git@github.com -o "StrictHostKeyChecking no" 2>&1 > /dev/null)
if [[ $git_user != *"successfully"* ]]
then
error_message+="Error: SSH authentication failed. Please verify SSH authentication.\n"
fi
git_user=$(echo $git_user | sed -e 's/.*Hi //' -e 's/You.*//')
# Check if repository already exists
if curl -s -H "Authorization: token $(cat ~/.github)" "https://api.github.com/repos/$git_user/$reponame" | grep -q "\"not found\""
then
error_message+="Error: Repository $reponame already exists.\n"
fi
# Check if local directory already exists
if [ -d ~/github/$reponame ]
then
error_message+="Error: Directory ~/github/$reponame already exists.\n"
fi
# Return error message if any error occurred
if [ ! -z "$error_message" ]
then
echo -e $error_message
exit 1
fi
}
create_repo() {
private="true"
if [ "$repo_type" = "public" ]; then
private="false"
fi
# Ensure the presence of ~/github directory
mkdir -p ~/github
# Create local directory and initialize it as a git repository
mkdir -p ~/github/$reponame && cd "$_"
git init > /dev/null 2>&1
# Create remote repository
curl -H "Authorization: token $(cat ~/.github)" https://api.github.com/user/repos -d "{\"name\":\"$reponame\",\"private\":\"$private\"}"
# Add the remote repository
git remote add origin https://github.com/$git_user/$reponame.git
echo "Successfully created the repository $reponame in ~/github/$reponame and as a remote repository at https://github.com/$git_user/$reponame"
if [ "$private" = "true" ]
then
echo "This is a private repository."
fi
echo
echo "You can now add files, commit, and push to populate the remote repository."
echo
exit 0
}
# Parse parameters
reponame=$1
repo_type=$2
if [[ "$1" = "--help" ]]; then
print_help
exit 0
fi
pre_reqs
create_repo