-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathba.sh
95 lines (80 loc) · 2.89 KB
/
ba.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
# --------------- BranchFixer ---------------
# --------------- version 0.1 ---------------
branch_edit() {
op_type=$1
executed_command=$2
branches=$(git branch --list | cut -c 3-)
found_master=false
found_main=false
while IFS= read -r branch_name; do
if [[ "$branch_name" == "master" ]]; then
found_master=true
elif [[ "$branch_name" == "main" ]]; then
found_main=true
fi
done <<<"$branches"
if $found_master; then
git "$op_type" master
fi
if $found_main; then
git "$op_type" main
fi
if ! $found_master && ! $found_main; then
echo "bash: main/master branches not found for command: ${executed_command}"
return 1
fi
}
branch_fixer() {
local exit_code=$?
# Retrieve the executed command using the history mechanism
local executed_command=$(history 1 | sed 's/^[[:space:]]*[0-9]*[[:space:]]*//')
if [[ $exit_code != 0 ]]; then
# Split the string based on whitespace
IFS=' ' read -ra strarr <<<"$executed_command"
# Print each value of the array by using a loop
for element in "${strarr[@]}"; do
if [[ $element == "git" ]]; then
case "$executed_command" in
*switch*)
branch_edit "switch" "$executed_command"
branch_edit_exit_code=$?
if [[ $branch_edit_exit_code -eq 0 ]]; then
echo "Corrected branch for git switch!"
fi
;;
*checkout*)
branch_edit "checkout" "$executed_command"
branch_edit_exit_code=$?
if [[ $branch_edit_exit_code -eq 0 ]]; then
echo "Corrected branch for git checkout!"
fi
;;
*merge*)
branch_edit "merge" "$executed_command"
branch_edit_exit_code=$?
if [[ $branch_edit_exit_code -eq 0 ]]; then
echo "Corrected branch for git merge!"
fi
;;
*rebase*)
branch_edit "rebase" "$executed_command"
branch_edit_exit_code=$?
if [[ $branch_edit_exit_code -eq 0 ]]; then
echo "Corrected branch for git rebase!"
fi
;;
*log*)
branch_edit "log" "$executed_command"
branch_edit_exit_code=$?
if [[ $branch_edit_exit_code -eq 0 ]]; then
echo "Corrected branch for git log!"
fi
;;
*) ;;
esac
fi
done
fi
}
PROMPT_COMMAND=branch_fixer
# --------------- BranchFixer ---------------