-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitus.sh
executable file
·164 lines (138 loc) · 3.77 KB
/
gitus.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
157
158
159
160
161
162
163
164
#!/bin/bash
############################################################
# Help #
############################################################
Help(){
# Display Help
echo "gitus is git but simpler."
echo
echo "gitus ([-m \"commit message\"] -s) | -r [repo_url] | -(h|l|u)"
echo "options:"
echo "h Print this Help."
echo "m Add a commit message, default is \"Update\"."
echo "s Send your changes to the cloud."
echo "r Receive changes from the cloud."
echo "l Activate/deactivate learning mode."
echo "u Update gitus."
echo
}
debug=$(git config --bool --default true gitus.debug)
Run(){
if [[ $debug = true ]]; then
echo "[gitus] $ $@"
fi
"$@"
}
git_vars_not_set_msg_shown=false
CheckVar(){
var=$1
message=$2
tmp=$(git config --global --get "$var")
if [ -z "$tmp" ]; then
if [ $git_vars_not_set_msg_shown = false ]; then
echo "[gitus] Oh no! you haven't set git global variables yet. Let's do that"
git_vars_not_set_msg_shown=true
fi
read -p "$message" tmp
Run git config --global "$var" "$tmp"
fi
}
CheckSSH(){
if [ ! -f ~/.ssh/id_rsa ] ; then
CheckVar "user.email" "[gitus] what is you'r email? "
email=$(git config --global --get "user.email")
echo "[gitus] Generating SSH Key"
Run ssh-keygen -t rsa -b 4096 -C "$email" -f ~/.ssh/id_rsa
Run eval "$(ssh-agent -s)"
Run ssh-add ~/.ssh/id_rsa
echo
echo "[gitus] ⚠️ Please add the SSH key to your account before proceeding. For more information"
echo "[gitus] GitHub: https://docs.github.com/en/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account"
read -p "[gitus] press Enter to continue "
fi
}
Init(){
CheckVar "user.name" "[gitus] what is you'r user name? "
CheckVar "user.email" "[gitus] what is you'r email? "
git config --global init.defaultBranch main # only 1 time !
}
Send(){
Init
CheckSSH
# search for .git repo
has_git_repo=$( ls -ap | egrep "^\.git/$" | wc -l )
if [ $has_git_repo = 0 ]; then
Run git init
if [ -z "$msg" ]; then
msg="first commit"
fi
fi
# the remote is not set for new repos and generated projet
if [ -z "$(git remote)" ]; then
repo_name="$(basename "$PWD")"
user_name="$(git config --global --get user.name)"
Run git remote add origin [email protected]:"$user_name"/"$repo_name".git
had_no_remote=true # to set default branch to main after commit
fi
if [ -z "$msg" ]; then
msg="Update"
fi
Run git add -A
# fixme:
echo "[gitus] $ git commit -m \"$msg\""
git commit -m "$msg"
if [[ $had_no_remote = true ]]; then
Run git branch -M main # needed only one time after first commit
fi
remote="$(git remote show)"
current_branch="$(git branch --show-current)"
Run git push -u "$remote" "$current_branch"
}
Receive(){
if [ -z "$clone_url" ]; then
Run git pull
else
Run git clone "$clone_url"
fi
}
while getopts "rhm:slu" flag;
do
case "${flag}" in
h) Help
exit
;;
m) msg=${OPTARG}
;;
s) echo "[gitus] Sending your changes to the cloud."
Send
;;
r) echo "[gitus] Receiving changes from the cloud."
eval nextopt=\${$OPTIND}
# echo "$nextopt"
# https://stackoverflow.com/questions/11517139/optional-option-argument-with-getopts
if [[ -n $nextopt && $nextopt != -* ]] ; then
OPTIND=$((OPTIND + 1))
fi
clone_url=${nextopt}
Receive
;;
l)
d=$(git config --bool --default true gitus.debug)
if [[ "$d" = true ]]; then
echo "[gitus] Learning mode is not active"
git config --global "gitus.debug" false
else
echo "[gitus] Learning mode is active"
git config --global "gitus.debug" true
fi
;;
u)
echo "[gitus] Downloading updates"
gitus_folder=$(dirname -- "$0")
cd $gitus_folder
git pull
;;
\?) # Invalid option
exit;;
esac
done