-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.sh
executable file
·129 lines (119 loc) · 2.87 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
#!/bin/bash
# Default values
commit_message="Automated commit"
branch="main"
new_branch=false
add_all=false
show_diff=false
debug_mode=false
# 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: dev)"
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 ""
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
;;
*)
break
;;
esac
done
# Check if any files are specified
if [[ $# -eq 0 && "$add_all" = false && "$show_diff" = 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