-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc_bashrc
More file actions
468 lines (405 loc) · 14 KB
/
Copy pathsrc_bashrc
File metadata and controls
468 lines (405 loc) · 14 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#!/bin/bash
add_to_path() {
local dir="$1"
[ -z "$dir" ] && return
case ":$PATH:" in
*:"$dir":*) ;;
*)
if [ -z "$PATH" ]; then
PATH="$dir"
else
PATH="$PATH:$dir"
fi
;;
esac
}
add_to_pkg_config_path() {
local dir="$1"
[ -z "$dir" ] && return
case ":$PKG_CONFIG_PATH:" in
*:"$dir":*) ;;
*)
if [ -z "$PKG_CONFIG_PATH" ]; then
PKG_CONFIG_PATH="$dir"
else
PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$dir"
fi
;;
esac
}
add_to_path "/c/msys64/usr/bin"
add_to_pkg_config_path "/c/msys64/mingw64/lib/pkgconfig"
add_to_path "$HOME/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0/LocalCache/local-packages/Python313/Scripts"
add_to_path "/c/Program Files/PostgreSQL/17/bin"
add_to_path "/f/.programs/Qt/5.15.2/msvc2019/bin"
add_to_path "/c/Users/junki/.cargo/bin"
add_to_path "/c/Users/junki/AppData/Local/Programs/Microsoft VS Code/bin/"
export PATH
export PKG_CONFIG_PATH
export PROMPT_DIRTRIM=3
# Bold
BRED="\033[1;31m" # Red
BGREEN="\033[1;32m" # Green
BYEL="\033[1;33m" # Yellow
BBLUE="\033[1;34m" # Blue
BPUR="\033[1;35m" # Purple
BCYAN="\033[1;36m" # Cyan
BLUE='\033[36m'
PUR='\033[35m'
YEL='\033[38;5;220m'
RED='\033[0;31m'
GREEN='\033[0;32m'
U_WHITE='\033[4;37m'
NC='\033[0m'
cur_dir=$(pwd)
cd ~
home_dir=$(pwd)
cd "$cur_dir"
tar_dir="${PROJECTS_ROOT:-$home_dir/Desktop/projects}"
mkdir -p "$tar_dir"
df_file="$tar_dir/.disable_fetch"
py_file="$tar_dir/.disable_win_py"
node_path="/c/Program Files/nodejs/node.exe"
if command -v winpty >/dev/null 2>&1; then
alias node="winpty \"$node_path\""
alias watch="winpty watch"
alias top="winpty top"
else
alias node="\"$node_path\""
fi
alias npm="npm.cmd"
alias npx="npx.cmd"
alias supabase="npx.cmd supabase"
alias mysql="mysql -uroot -p"
alias status="echo \"\" && date -r \"$tar_dir/.status\" && echo "" && cat \"$tar_dir/.status\""
reload() {
clear
# shellcheck disable=SC1090
"$home_dir/.bashrc"
}
running=true
bNewBr=false
bNewCO=false
bUnstg=false
declare -a pids=()
cleanup() {
if ! $running; then
return
fi
running=false
echo -e "\nCancelling fetch..."
if [ ${#pids[@]} -gt 0 ]; then
for job in "${pids[@]}"; do
if kill -0 "$job" 2>/dev/null; then
kill "$job" 2>/dev/null
fi
done
wait "${pids[@]}" 2>/dev/null
pids=()
fi
[ -d "$tmp_dir" ] && rm -rf "$tmp_dir" 2>/dev/null
cd "$home_dir"
}
git() {
if [ "$1" = log ]; then
shift
command git log --graph "$@"
else
command git "$@"
fi
}
git_chk_newBR(){
[ "$running" != true ] && return
# Load branch lists into arrays
local local_branches=() remote_branches=() new_branches=()
readarray -t local_branches < <(git branch --format="%(refname:short)" 2>/dev/null)
readarray -t remote_branches < <(git branch -r --format="%(refname:lstrip=3)" 2>/dev/null | grep -vE 'HEAD')
# Build a lookup table for local branches.
declare -A local_map
for branch in "${local_branches[@]}"; do
local_map["$branch"]=1
done
# Iterate remote branches and add those not in the lookup.
for rb in "${remote_branches[@]}"; do
if [[ ${local_map[$rb]:-0} -eq 1 ]]; then
continue
fi
new_branches+=("$rb")
done
# Print header if there are any new branches.
[ "${#new_branches[@]}" -lt 1 ] && return
local header prefix last_prefix i rb_item
if [ "$bNewCO" = false ] && [ "$bUnstg" = false ]; then
header="└──"
prefix="\t├──"
last_prefix="\t└──"
else
header="├──"
prefix="│\t├──"
last_prefix="│\t└──"
fi
echo -e "│"
echo -e "$header ${BLUE}[ BRANCHES ]${NC}"
for i in "${!new_branches[@]}"; do
rb_item="${new_branches[$i]}"
if [ "$i" -eq "$(( ${#new_branches[@]} - 1 ))" ]; then
echo -e "$last_prefix \"$rb_item\""
else
echo -e "$prefix \"$rb_item\""
fi
done
if [ "$bNewCO" = false ] && [ "$bUnstg" = false ]; then
echo ""
fi
}
git_chk_branches() {
[ "$running" != true ] && return
local updates=() unstaged=() current_branch
current_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
local STASH
STASH=$(git rev-list --walk-reflogs --ignore-missing --count refs/stash 2>/dev/null)
local branches=()
readarray -t branches < <(git branch --format="%(refname:short)" 2>/dev/null)
# Create temporary directory for parallel results.
local tmp_dir
tmp_dir=$(mktemp -d)
local pids=()
for branch in "${branches[@]}"; do
(
local branch_out="" UPSTREAM BEHIND AHEAD diffstat formatted_diff
# Get upstream; if not set, report it instead of modifying configuration.
UPSTREAM=$(git rev-parse --abbrev-ref "$branch@{upstream}" 2>/dev/null || echo "")
if [ -z "$UPSTREAM" ]; then
if git show-ref --verify --quiet "refs/remotes/origin/$branch"; then
echo "updates:'$branch' [ ${YEL}no upstream configured${NC} ]" >> "$tmp_dir/results.txt"
fi
exit 0
fi
BEHIND=$(git rev-list --count "$branch..$UPSTREAM" 2>/dev/null || echo 0)
AHEAD=$(git rev-list --count "$UPSTREAM..$branch" 2>/dev/null || echo 0)
# For the current branch, check for unstaged changes.
if [ "$branch" = "$current_branch" ]; then
diffstat=$(git diff HEAD --shortstat 2>/dev/null)
if [ -n "$diffstat" ]; then
if echo "$diffstat" | grep -q "changed"; then
formatted_diff=$(echo "$diffstat" | sed -E \
-e 's/([0-9]+) file(s)? changed/\1 file\2,/' \
-e 's/, ([0-9]+) (insertion|addition)s?\(\+\)/ +\1/' \
-e 's/, ([0-9]+) (deletion|deletions?)\(-\)/ -\1/')
else
formatted_diff="$diffstat"
fi
formatted_diff=$(echo "$formatted_diff" | sed -E "s/\+([0-9]+)/$(printf '%b' "$GREEN")+\1$(printf '%b' "$NC")/g")
formatted_diff=$(echo "$formatted_diff" | sed -E "s/ -([0-9]+)/ $(printf '%b' "$RED")-\1$(printf '%b' "$NC")/g")
echo "unstaged:'$branch' →$formatted_diff" >> "$tmp_dir/results.txt"
fi
fi
# Skip if no commit differences.
if [ "$BEHIND" -lt 1 ] && [ "$AHEAD" -lt 1 ]; then
exit 0
fi
if [ "$BEHIND" -gt 0 ] && [ "$AHEAD" -gt 0 ]; then
echo "updates:'$branch' [ ${RED}▼$BEHIND${NC} ${GREEN}▲$AHEAD${NC} ]" >> "$tmp_dir/results.txt"
elif [ "$BEHIND" -gt 0 ]; then
echo "updates:'$branch' [ ${RED}▼$BEHIND${NC} ]" >> "$tmp_dir/results.txt"
elif [ "$AHEAD" -gt 0 ]; then
echo "updates:'$branch' [ ${GREEN}▲$AHEAD${NC} ]" >> "$tmp_dir/results.txt"
fi
) &
pids+=($!)
done
for pid in "${pids[@]}"; do
wait "$pid"
done
# Gather results.
if [ -f "$tmp_dir/results.txt" ]; then
while IFS= read -r line; do
local type=${line%%:*}
local entry=${line#*:}
if [ "$type" = "updates" ]; then
updates+=("$entry")
elif [ "$type" = "unstaged" ]; then
unstaged+=("$entry")
fi
done < "$tmp_dir/results.txt"
rm -rf "$tmp_dir"
fi
local header prefix last_prefix i
if [ "${#unstaged[@]}" -lt 1 ]; then
header="└──"
prefix="\t├──"
last_prefix="\t└──"
else
header="├──"
prefix="│\t├──"
last_prefix="│\t└──"
fi
if [ "${#updates[@]}" -gt 0 ]; then
bNewCO=true
echo -e "│"
echo -e "$header ${PUR}[ COMMITS ]${NC}"
for i in "${!updates[@]}"; do
if [ "$i" -eq "$(( ${#updates[@]} - 1 ))" ]; then
echo -e "$last_prefix ${updates[$i]}"
else
echo -e "$prefix ${updates[$i]}"
fi
done
[ "${#unstaged[@]}" -lt 1 ] && echo ""
fi
if [ "${#unstaged[@]}" -gt 0 ]; then
bUnstg=true
echo -e "│"
echo -e "└── ${RED}[ UNSTAGED ]${NC}"
for i in "${!unstaged[@]}"; do
if [ "$i" -eq "$(( ${#unstaged[@]} - 1 ))" ]; then
echo -e "\t└── ${unstaged[$i]}"
else
echo -e "\t├── ${unstaged[$i]}"
fi
done
echo ""
fi
if [ "$STASH" -gt 0 ]; then
echo -e "* ${STASH} stash found.\n"
fi
}
############################################################################################################
# Check which python to use (WIN, MINGW)
############################################################################################################
if [ ! -f "$py_file" ] && { [ "$cur_dir" = "$home_dir" ] || [ "$cur_dir" = "$tar_dir" ]; }; then
echo "0" > "$py_file"
if command -v attrib >/dev/null 2>&1; then
attrib +h "$py_file" 2>/dev/null
fi
fi
if [[ "$OSTYPE" == "linux-gnu"* || "$OSTYPE" == "darwin"* ]]; then
py_type="LINUX"
else
if [ "$(cat "$py_file")" -eq "0" ]; then
export PATH="/c/Users/junki/AppData/Local/Microsoft/WindowsApps:$PATH"
alias python='winpty python.exe'
py_type="WINDOWS"
else
export PATH=$(echo $PATH | tr ':' '\n' | grep -v '/c/msys64/ucrt64/bin' | paste -sd:)
export PATH="/c/msys64/mingw64/bin:/c/msys64/mingw64/lib:$PATH"
if alias python >/dev/null 2>&1; then
unalias python
fi
py_type="MINGW64"
fi
fi
hash -r
sync
############################################################################################################
# Check directory
############################################################################################################
if [ "$cur_dir" = "$home_dir" ] || [ "$cur_dir" = "$tar_dir" ]; then
cd "$tar_dir"
elif [[ "${BASH_SOURCE[0]}" != "$0" ]]; then
return
fi
############################################################################################################
# Check if fetching is disabled
############################################################################################################
if [ ! -f "$df_file" ] && { [ "$cur_dir" = "$home_dir" ] || [ "$cur_dir" = "$tar_dir" ]; }; then
echo "0" > "$df_file"
if command -v attrib >/dev/null 2>&1; then
attrib +h "$df_file" 2>/dev/null
fi
fi
if [ -f "$df_file" ] && [ "$(cat "$df_file")" -eq "1" ]; then
return
fi
ping_flag="-n 1 -w 100"
if [[ "$OSTYPE" == "linux-gnu"* || "$OSTYPE" == "darwin"* ]]; then
ping_flag="-c 1 -W 0.1"
fi
if ! ping $ping_flag 1.1.1.1 &> /dev/null; then
echo -e "${BLUE}[INTERNET]${NC} ${RED}Unavailable${NC}"
echo -e "${PUR}[PYTHON]${NC} ${U_WHITE}$py_type${NC}"
cleanup
if [[ "${BASH_SOURCE[0]}" != "$0" ]]; then
return
else
exit 0
fi
fi
############################################################################################################
# Start of console printouts and main feaatures
############################################################################################################
echo -e "${YEL}[TIP]${NC} To change python type, \"echo (0: WIN, 1: MINGW) > .disable_win_py\", restart"
echo -e "${YEL}[TIP]${NC} To disable fetch, \"echo 1 > .disable_fetch\""
echo -e "${YEL}[TIP]${NC} To cancel fetch, \"ctrl-C\"\n"
echo -e "${BLUE}[INTERNET]${NC} ${GREEN}OK${NC}"
echo -e "${PUR}[PYTHON]${NC} ${U_WHITE}$py_type${NC}\n"
trap cleanup SIGINT SIGTERM
cols=$(tput cols)
total_repo=0
pass_repo=0
declare -a repo_list
tmp_dir=$(mktemp -d)
pids=()
start_time=$(date +%s)
for dir in "$tar_dir"/*; do
[ "$running" != true ] && [ "${BASH_SOURCE[0]}" != "$0" ] && return
[ ! -d "$dir/.git" ] && continue
repo_name=$(basename "$dir")
echo "Fetching: $repo_name..."
((total_repo++))
repo_list+=("$repo_name")
(
cd "$dir" || return
git -C "$dir" fetch --no-progress --all --prune > /dev/null 2>&1
if [ $? -ne 0 ] && [ -d "$tmp_dir" ]; then
echo -e "${RED}[ERROR] Failed to fetch repo: $repo_name! Ensure access!${NC}\n" > "$tmp_dir/$repo_name.log"
exit 1
fi
tmp_branch=$(mktemp)
tmp_new=$(mktemp)
git_chk_branches > "$tmp_branch" 2>&1
git_chk_newBR > "$tmp_new" 2>&1
if [ -d "$tmp_dir" ]; then
cat "$tmp_new" "$tmp_branch" > "$tmp_dir/$repo_name.log"
rm -f "$tmp_branch" "$tmp_new" > /dev/null 2>&1
fi
) &
pids+=($!)
done
if [ $total_repo -eq 0 ]; then
echo -e "${BLUE}* no previous fetch status.${NC}" > "$tar_dir/.status"
if [ "${BASH_SOURCE[0]}" != "$0" ]; then
return
else
exit
fi
fi
# Wait for all fetches.
for pid in "${pids[@]}"; do
wait "$pid"
[ $? -eq 0 ] && ((pass_repo++))
done
end_time=$(date +%s)
elapsed=$(($end_time - $start_time))
elapsed_sec=$elapsed
echo ""
if [ "$running" == true ]; then
# Group branch info by repo.
: > "$tar_dir/.status"
for repo in "${repo_list[@]}"; do
if [ -s "$tmp_dir/$repo.log" ]; then
echo -e "${YEL}[ $repo ]${NC}" >> "$tar_dir/.status"
cat "$tmp_dir/$repo.log" >> "$tar_dir/.status"
printf "%0.s─" $(seq 1 $cols) >> "$tar_dir/.status"
echo -e "\n" >> "$tar_dir/.status"
fi
done
cat "$tar_dir/.status"
if [ "$total_repo" -eq "$pass_repo" ]; then
echo -e "${GREEN}[SUCCESS]${NC} Fetched ${total_repo} repositories in ${elapsed_sec}s"
else
echo -e "${RED}[FAILED]${NC} Fetched ${pass_repo} of ${total_repo} repositories in ${elapsed_sec}s"
fi
fi
[ -d "$tmp_dir" ] && rm -rf "$tmp_dir" 2>/dev/null
running=false