-
Notifications
You must be signed in to change notification settings - Fork 0
/
createGitRepo.sh
executable file
·70 lines (57 loc) · 1.97 KB
/
createGitRepo.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
#!/usr/bin/bash
create_github_repo() {
# Checking whether current folder is a gig repository
if [ -d .git ]; then
echo "Already Git repository"
echo "Choose a new directory that is not a git repository and re-run the command"
exit
fi
# Collecting and handling repository name from user
while true;
do
# Prompt the user to enter the repository name
read -r -p "Enter the repository name: " REPO_NAME
# Check if the repository name is provided
if [ -z "$REPO_NAME" ]; then
echo "Repository name cannot be empty."
continue
fi
# Validate repository name
if [[ ! "$REPO_NAME" =~ ^[a-zA-Z0-9_-]+$ ]]; then
echo "Invalid repository name. Please use only letters, numbers, hyphens, and underscores."
continue
fi
# checking if folder is already a repository
if [ -d "$REPO_NAME" ];
then
echo "Directory with the same name already exists."
echo "Choose a different directory"
continue
fi
# If the repository name is valid, exit
break
done
# Creating and initializing new repository
mkdir "$REPO_NAME" && cd "$REPO_NAME" || exit
# Add README file and commit initial changes
touch README.md
readme_content="## $REPO_NAME\n\nRead me content will be added here.."
echo -e "$readme_content" > README.md
git init
git add .
git commit -m "Readme file added"
# Check if the repository exists on GitHub
response=$(curl -s -o /dev/null -w "%{http_code}" "https://api.github.com/repos/$GIT_USERNAME/$REPO_NAME")
if [ $response -eq 200 ];
then
# if repository exists on github, set it as url
echo "Repository $REPO_NAME already exists on GitHub."
git remote add origin "https://$GITHUB_TOKEN@github.com/$GIT_USERNAME/$REPO_NAME.git"
else
# Create the repository using the GitHub API
creatingRemoteRepo
fi
# Push to remote
pushingToRemote
echo "Repository setup completed successfully."
}