-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlimbo.sh
executable file
·111 lines (98 loc) · 2.07 KB
/
limbo.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
#!/usr/bin/env bash
# ------------
# Spinner
# ------------
sp='/-\|'
sc=0
spin() {
echo -en "\r$1 ${sp:sc++:1}"
((sc==${#sp})) && sc=0
}
endspin() {
printf '\r%s\n' "$@"
}
waitingForChange=0
conflictsToSolve=0
# ------------
# Wait
# ------------
wait() {
stopConflictBlock;
spin "Waiting for change"
waitingForChange=1
}
stopWait() {
if [ $waitingForChange -eq 1 ]; then
waitingForChange=0
endspin
fi
}
# ------------
# PULL
# ------------
pull() {
stopConflictBlock;
stopWait;
echo -e "\n-----------------------------";
echo "Pull";
git pull --rebase;
echo -e "-----------------------------\n";
echo -e "\n"
}
# ------------
# PUSH
# ------------
initialPush() {
echo -e "\n-----------------------------";
echo "Initial Push";
BRANCH_NAME=`git rev-parse --abbrev-ref HEAD`
git push -u origin $BRANCH_NAME;
echo -e "-----------------------------\n";
}
push() {
stopConflictBlock;
stopWait;
echo -e "\n-----------------------------";
echo "Push";
git push;
echo -e "-----------------------------\n";
}
# ------------
# Conflicts
# ------------
handleConflicts() {
stopWait;
if [[ $conflictsToSolve -eq 0 ]]; then
echo -e "\033[0;31mBEWARE ! No action possible, fix the divergence (possible rebase conflicts).\033[0m"
read -p "Would like to reset your local copy to match the remote one ? (y/N) " reset
if [ "$reset" == "y" ]; then
git reset --hard origin/$(git rev-parse --abbrev-ref HEAD)
fi
conflictsToSolve=1
fi
}
stopConflictBlock() {
if [ $conflictsToSolve -eq 1 ]; then
conflictsToSolve=0
fi
}
# ------------
# LOOP
# ------------
while(true);
do
git remote update &> /dev/null;
UPSTREAM=${1:-'@{u}'}
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse "$UPSTREAM")
BASE=$(git merge-base @ "$UPSTREAM")
if [ $LOCAL = $REMOTE ]; then
wait;
elif [ $LOCAL = $BASE ]; then
pull;
elif [ $REMOTE = $BASE ]; then
push;
else
handleConflicts;
fi
done;