-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitpush.sh
48 lines (40 loc) · 1.7 KB
/
gitpush.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
function gitpush {
commit_message=$1
github_username=$GITHUB_USERNAME
github_token=$GITHUB_TOKEN
# Check if a commit message is provided
if [ -z "$commit_message" ]; then
echo "Please provide a commit message."
return 1
fi
# Check if GitHub username is set
if [ -z "$github_username" ]; then
echo "GitHub username is not set."
read -p "Enter your GitHub username: " github_username
export GITHUB_USERNAME=$github_username
fi
# Check if GitHub token is set
if [ -z "$github_token" ]; then
echo "GitHub token is not set."
read -p "Enter your GitHub token: " -s github_token
export GITHUB_TOKEN=$github_token
fi
# Check if .git folder exists in the current directory
if [ ! -d ".git" ]; then
echo "Git repository not found in the current directory."
return 1
fi
# Read repository name from .git/config file
repository=$(git config --get remote.origin.url | grep -oP '.*/\K[^/]*(?=\.git)')
# If repository name is not found, exit
if [ -z "$repository" ]; then
echo "Repository name not found."
return 1
fi
# Build URL with username/token and repository name
url="https://$github_username:[email protected]/$github_username/$repository"
# Execute Git commands
git add . || { echo "Failed to add files."; return 1; }
git commit -m "$commit_message" || { echo "Failed to commit changes."; return 1; }
git push "$url" || { echo "Failed to push changes."; return 1; }
}