-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.sh
executable file
·156 lines (145 loc) · 3.47 KB
/
git.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
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
# Default values
commit_message="Automated commit"
branch="main"
new_branch=false
add_all=false
show_diff=false
debug_mode=false
set_credentials=false
username=""
useremail=""
# Define help menu
help_menu() {
echo "Usage: ./git-script.sh -aDd -b <branch> -n <name> -c <msg>"
echo ""
echo "OPTIONS:"
echo " -a, --all add all changes"
echo " -b, --branch <name> specify branch (default: main)"
echo " -n, --new <name> create new branch and switch to it"
echo " -c, --commit <msg> custom commit message (default: Automated commit)"
echo " -D, --debug enable debug mode"
echo " -d, --diff show git diff --stat before committing or when it's the only argument"
echo " -h, --help display this help menu"
echo " -u, --username <name> set Git username"
echo " -e, --email <email> set Git email"
echo ""
echo "This script was generated by ChatGPT, a language model trained by OpenAI."
echo ""
exit 1
}
# Parse arguments
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-a | --all)
add_all=true
shift
;;
-b | --branch)
branch="$2"
shift
shift
;;
-n | --new)
branch="$2"
new_branch=true
shift
shift
;;
-c | --commit)
commit_message="$2"
shift
shift
;;
-D | --debug)
debug_mode=true
shift
;;
-d | --diff)
show_diff=true
shift
;;
-h | --help)
help_menu
;;
-u | --username)
username="$2"
set_credentials=true
shift
shift
;;
-e | --email)
useremail="$2"
set_credentials=true
shift
shift
;;
*)
break
;;
esac
done
if [ "$set_credentials" = true ]; then
if [ -n "$username" ]; then
git config user.name "$username"
fi
if [ -n "$useremail" ]; then
git config user.email "$useremail"
fi
fi
# Check if any files are specified
# Skip help menu if credentials are being set but no other actions are specified
if [[ $# -eq 0 && "$add_all" = false && "$show_diff" = false && "$set_credentials" = false ]]; then
help_menu
fi
# Create new branch if option is provided
if [ "$new_branch" = true ]; then
git checkout -b "$branch"
fi
# Show git diff --stat and skip all other steps
if [ "$show_diff" = true ] && [[ "$#" -eq 0 ]]; then
git diff --stat
exit 0
fi
if [ "$add_all" = true ]; then
git add --all
else
if [[ $# -gt 0 ]]; then
git add "$@"
fi
fi
if [ "$debug_mode" = true ]; then
echo "Debug Mode:"
echo " PWD: $(pwd)"
echo " Commit Message: $commit_message"
echo " Branch: $branch"
echo " New Branch: $new_branch"
echo " Add All: $add_all"
echo " Files: "
git status --short | awk '{print $2}'
echo ""
echo " Number of Files: $#"
echo " Show Diff: $show_diff"
echo " Debug Mode: $debug_mode"
echo ""
fi
if git diff-index --quiet HEAD --; then
echo "No changes to commit. Skipping the commit step."
else
if [ "$commit_message" != "Automated commit" ]; then
username=$(git config user.name)
commit_message+=" ($(date +%Y-%m-%d)) [branch: $branch, author: $username]"
git commit -m "$commit_message"
echo "Changes committed with message: $commit_message"
if [ "$add_all" = true ] && [ "$commit_message" != "Automated commit" ]; then
git push origin "$branch"
echo "Changes pushed to $branch branch."
fi
else
echo "Warning: No commit message set. Use the -c or --commit option to provide a commit message."
fi
fi
if [ "$new_branch" = true ]; then
git checkout dev
fi