-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitpilot.zsh
237 lines (208 loc) · 8.11 KB
/
gitpilot.zsh
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/zsh
gitpilot_check() {
if [ ! -d "$(pwd)/.git" ]; then
echo -e "${RED}.git folder not found${NC}"
return 1
fi
if [ ! -f "$(pwd)/.gitpilot/gp.json" ]; then
echo -e "${RED}gp.json file not found in .gitpilot folder${NC}"
return 1
fi
return 0
}
gitpilot() {
local_folder="/Users/$USER/gitpilot/$2/"
current_file=0
if [[ "$1" == "v" || "$1" == "-v" ]]; then
echo -e "${BCYAN}GitPilot${NC}"
echo -e "${BOLD}v.0.3.0${NC}"
echo -e "${YELLOW}JAP plugin${NC}"
fi
if [[ "$1" == "init" ]];then
mkdir -p "$(pwd)/.gitpilot/"
GP="$(pwd)/.gitpilot/gp.json"
if [ -e $GP ];then
echo -e "${RED}The gp.json already exists${NC}"
else
touch $GP
echo $(cat "${JAP_FOLDER}plugins/packages/gitpilot/config/gp.json") > $GP
echo -e "${GREEN}configure your gp.json${NC}"
echo -e $GP
fi
fi
if [[ "$1" == "deploying" || "$1" == "dep" ]]; then
commit_hash=$(git rev-parse HEAD)
GP="$(pwd)/.gitpilot/gp.json"
if [[ ! "$2" == "" ]];then
if [ -e "$(pwd)/.gitpilot/$2.json" ];then
GP="$(pwd)/.gitpilot/$2.json"
else
echo -e "${RED}$2.json not found in .gitpilot${NC}"
fi
fi
target_folder="$HOME/gitpilot/$commit_hash"
temp_directory="$HOME/gitpilot/$commit_hash/temp/"
if [ -d $target_folder ]; then
rm -r $target_folder
fi
extra_list=$(jq -r '.extra | .[]' "$GP")
current_path="$(pwd)"
if [[ ! $extra_list == "" ]]; then
while IFS= read -r iteme; do
if [ -e "${current_path}/${iteme}" ]; then
relative_path="${iteme#$current_path/}"
destination_folder="${target_folder}/${relative_path%/*}"
mkdir -p "$destination_folder"
cp -r "$iteme" "$destination_folder"
echo "- e $iteme";
else
echo -e "${BYELLOW} Warning(extra): ${NC}${YELLOW} Directory or File not found: ${iteme}/${item}${NC}"
current_file=$((current_file + 1))
fi
done <<< "$extra_list"
fi
mkdir -p "$temp_directory"
git checkout -b temp_branch "$commit_hash" > /dev/null 2>&1
changed_files=$(git diff --name-only HEAD^)
echo "$changed_files" | tar czf "$temp_directory.tar.gz" --files-from -
tar xzf "$temp_directory.tar.gz" -C "$temp_directory" > /dev/null 2>&1
cp -r "$temp_directory" "$target_folder"
rm -rf "$target_folder/.tar.gz"
git checkout - > /dev/null 2>&1
git branch -D temp_branch > /dev/null 2>&1
echo -e "${GREEN}Everything was provided${NC}"
gitpilotPush $target_folder $temp_directory $GP
fi
if [[ "$1" == "deploying-all" || "$1" == "dep-all" ]]; then
commit_hash=$(git rev-parse HEAD)
GP="$(pwd)/.gitpilot/gp.json"
if [[ ! "$2" == "" ]];then
if [ -e "$(pwd)/.gitpilot/$2.json" ];then
GP="$(pwd)/.gitpilot/$2.json"
else
echo -e "${RED}$2.json not found in .gitpilot${NC}"
fi
fi
target_folder="$HOME/gitpilot/$commit_hash"
temp_directory="$HOME/gitpilot/$commit_hash/temp/"
if [ -d $target_folder ]; then
rm -r $target_folder
fi
extra_list=$(jq -r '.extra | .[]' "$GP")
current_path="$(pwd)"
if [[ ! $extra_list == "" ]]; then
while IFS= read -r iteme; do
if [ -e "${current_path}/${iteme}" ]; then
relative_path="${iteme#$current_path/}"
destination_folder="${target_folder}/${relative_path%/*}"
mkdir -p "$destination_folder"
cp -r "$iteme" "$destination_folder"
echo "- e $iteme";
else
echo -e "${BYELLOW} Warning(extra): ${NC}${YELLOW} Directory or File not found: ${iteme}/${item}${NC}"
current_file=$((current_file + 1))
fi
done <<< "$extra_list"
fi
mkdir -p "$temp_directory"
git archive --format zip --output "$temp_directory.zip" "$commit_hash"
unzip "$temp_directory.zip" -d "$temp_directory" > /dev/null 2>&1
cp -r "$temp_directory" "$target_folder"
rm -rf "${target_folder}/.zip"
echo -e "${GREEN}Everything was provided${NC}"
gitpilotPush $target_folder $temp_directory $GP
fi
if [[ "$1" == "push" || "$1" == "push-all" ]]; then
if ! gitpilot_check; then
return 1
fi
startPush="$(date +"%Y-%m-%d %H:%M:%S")"
echo "push date: $startPush"
if [[ "$2" == "-gp2" ]];then
if [[ "$1" == "push-all" ]]; then
gitpilot dep-all "$3"
else
gitpilot dep "$3"
fi
else
if [[ "$1" == "push-all" ]]; then
gitpilot dep-all "$2"
else
gitpilot dep "$2"
fi
fi
fi
}
gitpilotPush() {
GP="$3"
temp_directory="$2"
target_folder="$1"
echo -e "${BLUE}reading $GP${NC}"
username=$(jq -r '.username' $GP)
server_ip=$(jq -r '.server_ip' $GP)
target_directory_on_server=$(jq -r '.target_directory_on_server' $GP)
ssh_password=$(jq -r '.ssh_password' $GP)
ignore_list=$(jq -r '.ignore | .[]' "$GP")
if [[ ! $ignore_list == "" ]];then
while IFS= read -r item; do
if [[ "$item" == *"*"* ]]; then
pattern="${item/\*/}"
found_files=$(find "$target_folder" -type f -name "$pattern")
if [ -n "$found_files" ]; then
find "$target_folder" -type f -name "$pattern" -delete > /dev/null 2>&1
else
echo -e "${BYELLOW} Warning(ignore): ${NC}${YELLOW} No files found matching the pattern \"$item\".${NC}"
current_file=$((current_file + 1))
fi
else
if [ -d "${target_folder}/${item}" ]; then
rm -rf "${target_folder}/${item}" > /dev/null 2>&1
else
echo -e "${BYELLOW} Warning(ignore): ${NC}${YELLOW} Directory not found: ${target_folder}/${item}${NC}"
current_file=$((current_file + 1))
fi
fi
done <<< "$ignore_list"
fi
rm -r $temp_directory
if [ -d "${target_folder}/.gitpilot" ]; then
rm -r "${target_folder}/.gitpilot"
fi
echo "Changed files:"
echo ""
find "${target_folder}" -type f | while read -r file; do
echo -e "${UNDERLINE}${file//$target_folder/}${NC}"
done
total_files=$(find "$target_folder" -type f | wc -l)
target_folder="${target_folder}/"
total_files=${total_files//" "/}
echo ""
echo -ne "${BOLD}Push on server ...${NC}"
sshpass -p "$ssh_password" scp -r "$target_folder"* "$username"@"$server_ip":"$target_directory_on_server"
echo -ne "\r\033[K${GREEN}${BOLD}Push on server Done${NC}"
echo ""
endPush="$(date +"%Y-%m-%d %H:%M:%S")"
start_timestamp=$(date -jf "%Y-%m-%d %H:%M:%S" "$startPush" "+%s")
end_timestamp=$(date -jf "%Y-%m-%d %H:%M:%S" "$endPush" "+%s")
duration=$(echo "$end_timestamp - $start_timestamp" | bc)
hours=$((duration / 3600))
minutes=$(( (duration % 3600) / 60 ))
seconds=$((duration % 60))
duration_formatted=""
if [ $hours -gt 0 ]; then
duration_formatted="${hours}h"
fi
if [ $minutes -gt 0 ]; then
duration_formatted="${minutes}m"
fi
if [ $seconds -gt 0 ]; then
duration_formatted="${seconds}s"
fi
if [[ ! $current_file == 0 ]];then
echo -e "${BYELLOW} Warning: ${NC}${YELLO} ${current_file}${NC}"
fi
echo ""
echo -e "${BCYAN}GitPilot${NC} $total_files${BLUE} files were transferred.${NC}"
echo -e "push duration: ${duration_formatted}"
rm -r $target_folder
}